Today I decided to run a quick experiment with Rust and WebAssembly. Here is a write up of what I learned.
Until this morning I had never looked at Rust, but after experimenting with Balzor and WebAssembly in a previous article, I was curious about how Rust’s offering compares to the .Net Blazor implementation.
On the surface Blazor and Rust are similar in that they both compile to wasm, but one key difference is that Rust doesn’t come with a huge runtime dependency like Blazor does. This results in a much more nimble application with a much smaller footprint. As a result I can definitely see a relatively big difference in startup time between the two. If you read my Blazor article you may remember that I used Blazor to create a simple bio section for my blog. Instead of repeating the same exact experiment, I decided to use Rust to create a “recent article” section for my blog.
The Recent article section is fairly simple. Basically all it does is fetch a list of recent articles and stamp out an html view on the page.
Let’s take a look at the Rust code:
Rust definitely has a learning curve to it, mostly because of syntax that I’m not used to as well as a new package manager system called crates.
For application bundling I opted to go with Rollup combined with the @wasm-tool/rollup-plugin-rust plugin.
Rollup outputs two files; a .js bundle file and .wasm file that will be loaded by the browser at runtime. For performance reasons I put both of these files behind my CDN.
Now that I have two WebAssembly features deployed as part of my blog it might be fun to compare the load times of both.
The Blazor component is a tad more complicated since it includes routing, but in principle both features should be comparable.
If you are interested in comparing performance, I have included the links to both below:
In both cases I am loading all file dependencies from a CDN, but the large payload size of the Blazor implementation leads to a much longer startup time. That said, you only pay the startup tax on the first request. Subsequent requests will load the .Net dependencies directly from browser cache.
I think Blazor will reduce the payload over time, but at the time of writing the gap is almost a little bit insane at 92K for Rust and 3.3MB for Blazor after gzip compression.