WebAssembly is amazing, even with its MVP version it has already opened new avenues for what is possible on the web in terms of performance, portability and security. In this article I will talk about WebAssembly and what makes it so revolutionary but I will also walk you through some of the use cases where WebAssembly, at least in its current version, would not function as fast.
Interpreters make the translation line by line and as the code is being executed, but compilers take their time ahead of time and make a more optimized translation. So Just in Time compilation literally means, make a more optimized translation through compilers, but doing that in time and as the code is being executed.
What WebAssembly does, is that it takes a statically typed, low level language like C/C++/Rust and compiles the code ahead of time. So you ship a .wasm file which is an assembly like format to the browser. Your browser doesn’t need to go through all the hassles explained above, it just executes the .wasm file.
But to be precise, WebAssembly is not a physical machine code, it is rather a conceptual machine code. And that is a deliberate design decision to gain portability. So you actually compile your code to an intermediate representation (IR), and every browser does the last step of translation from IR to the physical machine code locally. But wasm instructions are designed in a way that makes this last piece of translation very fast.
So far we learned why and how WebAssembly becomes performant, portable and secure and this already opens a lot of opportunities to build more complicated products on the web. But there is another aspect of WebAssembly that also makes me excited and it is that you can take advantage of all the code you have built in C/C++ and just ship it to the browser.
There are also a lot of amazing developers with many years of experience in building secure and scalable code in C/C++ and we can start using this pool of talent on the web!
A lot of companies have already started doing so. A famous example is AutoDesk, that has started moving a version of AutoCad to the web. AutoCad was first released in 1980s and it is an essential software for designers and architects. Being able to use the same code base and deliver the product on the web has been a huge win for AutoDesk.
WebAssembly is in its MVP version right now. The potentials are huge and the team is working to expand the features. But in its current version, there are a few things you cannot do with WebAssembly:
1- WebAssembly doesn’t support garbage collection (GC). So right now, you need to manage your own memory in C/C++ code, but GC support is on its way, and could be released in the future versions.
2- WebAssembly doesn’t support multi-threading right now, it is one of the first features to be released. Firefox even released this shortly but had to take it back because of the security issues in the SharedArrayBuffers.
I am hoping that we can start taking advantage of WebAssembly in our current product as well as our future architecture designs.
If you want to learn more about WebAssembly I suggest following Lin Clark on her twitter/Mozilla hack account. She is an amazing engineer and educator and has a lot of talks and articles about this topic.