If you're talking about ergonomics, I'm pretty sure that it's clear that WebAssembly is a better, way better ergonomics. The code is way shorter and the binding example I showed you requires pretty much nothing to convert the native Rust function into WebAssembly. It is able to convert basic types like integer and floats. Neon on the other hand needs a glue layer to convert your Rust and JavaScript into Rust. So the ergonomics suffer a bit with the native, native example compared to the WebAssembly one.
If we talk about stdlib, which is everything, the system networking, if you want to access the operating system, then your only way to do this is using Rust native modules. Because WebAssembly cannot access standard libraries, so you can't access file system, you can't access networking, unless you're willing to wait for a WebAssembly system interface, which is still in development, but they are working on making it accessible to WebAssembly as well.
Portability, it works on my machine, so every developer ever, you always need to remember native code is machine dependent, so if you compile it on macOS, you will not be able to run it on Windows, so if you're developing on macOS and your production environment is bare Linux containers, you need to compile it for bare Linux containers, so you can use Docker for that. WebAssembly, on the other hand, if I compile my code in WebAssembly and I give you the executable, you will be able to run it on your machine browser that supports WebAssembly, which is not true for native models that compile it on my machine. Good chance that you won't if your machine is different. For example, if you have an Intel-based Mac and I have an ARM-based Mac, then it needs to be recompiled, especially if you have a Windows or Linux machine, it needs to be compiled as well.
And if we talk about entry barrier for new developers, I think that WebAssembly is easier. If you want to introduce performance code in your Node.js ecosystem, I suggest you go to WebAssembly because native models can be written only in C, C++ or Rust. Those are pretty complex languages to grasp for beginners, for junior people. I'm not saying it's impossible, but the investment will be pretty big if you just want to rewrite one or two functions into native performance. Wasm, while they can be compiled from C, C++ or Rust, there is also partial support for Python, Ruby and Go. And moreover, you have the specialized language called AssemblyScript that is dedicated to to Wasm, so you can always start with AssemblyScript and go and get your WebAssembly version, and if you need to squeeze every possible inch of performance, you can always then introduce Rust. If we're thinking about Node.js, or browser, even though we're more focused on Node.js here, JavaScript is still king in the browser, and sometimes we need to run things in the also need to remember that native models can't be used in browsers, because JavaScript does not have support for foreign function interface. So native models can be used only in Node.js but not in the browser. WebAssembly, on the other hand, can be used in every place where you have a WebAssembly virtual machine available. So it's all the Node.js versions, plus all the browsers, except Internet Explorer. I'm sorry if you need to work with Internet Explorer, but other than Internet Explorer, the WebAssembly support is across the entire stack, including mobile browsers. So see, if you have a code that you share between the backend and the frontend, your pretty much solution is to go in the WebAssembly. If you want to squeeze performance only from your backend code, you can look into REST native models.
Here we have the summary, I will leave it to you to read, and I will just want to outline two main points. In my opinion, native models are meant to extend the Node.js with performance code. So if you want performance improvement into your Node.js ecosystem, native models is the way to do that. And WebAssembly, in my opinion, is a way to replace non-performant JavaScript pieces of code. So if you have a non-performant function, I suggest you try to rewrite it in WebAssembly whether through Rust that is compiled to WebAssembly or by using AssemblyScript. And if you want to add performance code to your Node.js, you want to extend your Node.js ecosystem, then you can look at native models with Rust for example. Thank you very much. You can find me on Twitter, GitHub, LinkedIn, and you can scan the QR code to take you to my blog. You can find links to two articles that this talk is based on. On my GitHub, you can find a link to the GitHub repositories for both the Rust and WebAssembly versions, which you can clone, you can run, you can play with them, you can experiment. Thank you for listening. I hope you learned something new. It can be performant not by itself, but with the help of Rust. So I hope you will find use for your new knowledge.
Comments