What exactly is WebAssembly?
WebAssembly, often abbreviated as Wasm, isn̵
Because Wasm was designed from the ground up, it was designed for performance. It removes the complex prototyping of JS and introduces the right basic elements. JS only has floating point numbers for numbers – any math operation in JS is done with floating point numbers, although floating point numbers are much slower and unnecessary in most cases. Wasm provides basic elements for ints and access to memory at the byte level. It has a text representation called WebAssembly Text (
.wat) with which you can code directly. You will always package it in its binary format for use on the web. Under the hood, Wasm runs on a stack-based virtual machine that translates the instructions into actual code that runs on your machine.
Since it is just an instruction format, the magic of Wasm is that it can be used as a portable compilation target for other languages without sacrificing the speed you would have while transpiling. This includes languages like C ++ and Rust which can now be run on the web when compiled into Wasm.
If your mind has just been blown it’s not theoretical – it actually works in practice. AutoDesk was able to port AutoCAD, a 30-year-old C ++ code base, to a WebAssembly-based web app with relative ease in just a few months. Native apps ported to the web are a major use case for Wasm, and we will see more and more apps take this approach as Wasm pushes the capabilities of the browser.
If you want to work with Wasm, you can try it out on an online WebAssembly playground.
JS is also much more accessible, as demonstrated by the success of client-side JS frameworks like React. Web apps created with React are still quite fast and, with good render optimization, can be run on most smartphones at 60 FPS without jerks.
Wasm on the desktop
This may seem a little uninteresting at first, but Wasm running on the server is a lot more reasonable than you think. WASI, or WebAssembly System Interface, standardizes Wasm’s interaction with the system and provides a modular environment in which WebAssembly can do all of its magic outside of the browser, either on your desktop or in a server environment. With WASI, developers can write the same app for native and web applications without running more instances of Chrome with Electron.
In a way, this replaces a lot of what Docker does. Docker founder Solomon Hykes even said so. Code written in any language (which sets this apart from .NET / JVM) can likely be compiled to Wasm and run on any server running WASI. It abstracts the code from the computer (no longer compiling for a specific CPU or operating system) and only requires WASI to be compiled and installed on the machine it is running on. While Docker isn’t going away either, it may be able to run Linux, Windows, and WASI containers side by side in the future.
This is similar to the promise made by Java: the runtime environment runs the same code on a Windows or Linux host. However, Wasm’s selling point is that you don’t have to program in Java and you are free to choose more powerful languages like C ++ and Rust.