Fast, safe, portable and serverless Rust functions as services

Search bytes

This example creates two ArrayBuffers using Javascript.

var buffer_1 = new ArrayBuffer(50000);
var buffer_2 = new ArrayBuffer(1000000);
var needle = new Uint8Array(buffer_1);
var haystack = new Uint8Array(buffer_2);
needle.fill(111)
haystack.fill(222)


In this first example, the needle is 50000 bytes long and the haystack is 1 million bytes long.
Also, as you can see, the needle will not be in the haystack because the needle and the haystack are filled with different data i.e. [111, 111, 111 ... 111, 111]vs [222, 222, 222 ... 222, 222]
When you click this button, this page sends these two byte arrays away to a FaaS which will see if the needle is in the haystack
If the sequence of bytes from the needle are NOT present in the haystack, then the result below will be Absent.



Result ...


Search bytes II

This example creates two much bigger ArrayBuffers using Javascript.

var buffer_1 = new ArrayBuffer(500);
var buffer_2 = new ArrayBuffer(1000000);
var needle = new Uint8Array(buffer_1);
var haystack = new Uint8Array(buffer_2);
needle.fill(111)
haystack.fill(111)


In this second example, the needle is 500 bytes in length, the haystack is one million bytes long, and the data is present.
As you can see, they are both filled with the same data [111, 111, 111 ... 111, 111] vs [111, 111, 111 ... 111, 111]
When you click this button, this page sends these two byte arrays away to a FaaS which will see if the needle is in the haystack
If the sequence of bytes from the needle are present in the haystack, then the result below will be Present.



Result!

This work is explained in more detail in an online article called An easy way to boost your client-side Javascript using WebAssembly(Wasm). An additional follow-up article called How to detect a sequence of bytes, in a byte array, using client-side Javascript also provides additional details about this work.