What I want to do is to delegate some heavy tasks to Rust and WASM.
Tasks like array filter, sort, map, ... and also async.
In my POC project I tried a first basic thing : filter an array using native JS and filter using Rust+WASM (js call Rust function).
Inside Rust, I used js_sys.
However, I find that native JS filter is more faster than Rust+WASM :
Result (chrome) :
Call to JS filter took 0.05499999679159373 milliseconds.
Call to Rust collect_numbers took 2.219999994849786 milliseconds.
Result (safari) :
Call to JS filter took 0 milliseconds.
Call to Rust collect_numbers took 4.999999999999886 milliseconds.
Result (firefox) :
Call to JS filter took 0 milliseconds.
Call to Rust collect_numbers took 4 milliseconds.
I tried, an array length 10, 100 and then 452, but always JS is more faster.
Have I missed up a configuration in my project ? Is there a particular recommendation (api, config, a way to do, ...) ? In which cases (Rust + WASM + JS) are recommended ?
These arrays are very small, and the filtering logic is very simple. I think in this case it's to be expected that doing it in JS is much faster compared to having to call some external code and incurring whatever overhead associated with doing so. If the array length was in the thousands, tens of thousands or more, and the filtering logic was complex, I could see the WASM solution being faster. I think you have the right idea to do heavy tasks in WASM, but the task in this example is very light.
Arrays in general are allocated pretty quickly as it's just a contiguous block of memory, and the allocation time between something like length 8 vs length 8k is not likely to matter much in the grand scheme of things if only done once or a couple of times.
Try more complex data, e.g. composed of various nested types (each of which must be instantiated separately), or something much more computationally expensive.
Okay, I will try, but in a real case, I don't think that in frontend side we do something much more computationally expensive, this will be the role of Backend or BFF (like graphql). I don't see very well the added value of WASM in frontend side.
Here's an example of a pretty cool Wasm project to showcase what it's capable of: idTech 4 WebAssembly port - Doom 3 Demo
If you're only doing things that are already normally taken care of by the frontend the value of Wasm is probably not that high. One thing to consider is that you could potentially move some of the computation done in the backend to the frontend and lighten the load on the server.
A good comparison might be parallelism: it can provide a big speedup in some cases, but in simple cases the overhead just ends up slowing the entire thing down.
I have done only some frontend usual tasks, if I choose to move some backend parts inside frontend, I will have a very huge bundle that will take tooooooo long time to be downloaded especially for devices with some poor performances.
Yeah I loved safety when using Rust + WASM (everything is check before frontend consumption) but I will loose performance.
I don't think that Rust + WASM are suitable for frontend apps that doesn't do 3D, image/video processing, VR, ... Even for these apps we should compare to see the added value.
Thank you so much for all your answer and contribution. I understand now very well when WASM and when not !