Using WebAssembly to turn Rust crates into fast TypeScript libraries
rybicki.io
rybicki.io
In our experience, the cost of boundary-crossing between wasm and js make it slower than js implementation for everything dom-related.
There are a few wasm libraries like finos/perspective and duckdb-wasm that are really useful in the client even though they don't interact with the dom
> These micro benchmarks show that there is also no such thing as free lunch in the browser. We are paying for the increased processing efficiency in WebAssembly with a sightly less efficient evaluation on very small input. Our recommendation is therefore to use DuckDB-Wasm if you need SQL, the features or the raw speed on medium to large data sizes. Stick to existing frameworks if your dataset is very small or if your queries only contain simple scans and filters.
In this case, the main thing that was "fast" was that I didn't have to rewrite a nontrivial string formatting library in a new language (I literally couldn't find any other JavaScript libraries that had this kind of error-formatting functionality, so that would have been my only other option). :-)
For node, it would be interesting to compare performance, weight, and gotchas of the wasm approach vs. building a shared library with Rust and then pulling it in through node-gyp. Loading wasm is certainly a lot simpler, but multithreading support seems shaky and I imagine there may be some other drawbacks compared compiling into node directly, despite the added complexity.
See: https://hacks.mozilla.org/2018/10/calls-between-javascript-a...
See: https://krausest.github.io/js-framework-benchmark/current.ht...
That doesn't support your statement here, so I was wondering what takeaway you meant to direct our attention to when providing that link.
Either way it's clear that even a compile time language with no garbage collection can't get faster than the DOM itself allows, which is the crux of my point. In theory a compiled language should run at least twice as fast as JS, but it's not the case in the browser.
They are both _way_ faster than React, and they are both faster than Vue and Svelte by a larger magnitude than SolidJS is faster than either of them.
That is not because Dioxus/leptos have less features than Yew. I'd actually argue they are more feature-full [1].
For icing on the cake: the most "bare-bones" WASM/Rust based entry to the benchmarks, "sledge-hammer", is as fast as the most bare-bones JS entry.
There are valid reasons for not using a Rust frontend framework (over-the-wire size, compilation speed, ecosystem, ..etc), but execution/DOM-manipulation speed is not one of them.
[1] I've used leptos, and tried out Yew, and have read through the docs of all three.
Leptos and Dioxus, Rust/WASM frameworks, are competitive with SolidJS, and right out destroy React, when it comes to manipulating the DOM!
So I'd love to hear, if possible, about when was it that you evaluated your WASM based solution, what language and framework you used, etc.
[0] https://krausest.github.io/js-framework-benchmark/2023/table...
This was last year though, maybe things got better now, so thanks for the pointers, will do another attempt!
If you're having another go, I'd recommend checking out sledge-hammer[0], a wasm-bindgen alternative that the folks behind Dioxus have been working on. It's a faster, but more limited version. Best of luck!
Indeed, string decoding may have been our bottleneck as explained in [0].
Will definitely have a look.
PS: Evan from https://github.com/Demonthos, if you ever stumble upon this there is typo in the Dioxus link from your bio :)
Our team just uses wasm-bindgen though, and not wasm-pack; curious how much additional value you get from the latter? I could be mistaken but my impression is that wasm-pack uses Webpack; we've generally been trying to use esbuild instead (to make build times faster) but I haven't used wasm-pack beyond running the tutorial so maybe I don't know what I'm missing.
I remember reading something about doing something similar to this but it creates libraries for multiple languages. Have you done something similar before or do you have an idea where to look?