What exactly is WebAssemble?
Because Wasm was designed from the ground up, it is built with performance in mind. It removes the complex prototype from JS and introduces correct primitives. JS has only floats for numbers – every math operation in JS is done with floats, although float mathematics is much slower and unnecessary in most cases. Wasm provides primitives for ints, and provides byte-level access to memory. It has a text representation called WebAssembly Text (
.wat), which you can use to encode directly. You always unpack it in its binary format for use on the web. Under the hood, Wasm runs on a stack-based virtual machine, which translates the instructions into the actual code running on your machine.
Since it is only an instructional 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 with transpiling. This includes languages such as C ++ and Rust, which can now be run on the web when compiled into Wasm.
If your mind was just blown up, it’s not theoretical – it actually works in practice. AutoDesk was able to port AutoCAD, a 30-year C ++ code base, to a WebAssembly-based web app, in a matter of months, relatively easily. Built-in apps that are transferred to the web are an important use case for Wasm, and we’re starting to see more apps taking this approach when Wasm runs what’s possible in the browser.
If you want to get started with Wasm, you can go to an online playground for WebAssemble to try it out.
JS is also much more accessible, as evidenced by the success of JS frames on the client side such as React. Web apps built with React are still quite fast and with good rendering optimization, they can run at 60 FPS without jank on most smartphones.
Wash on the desk
This seems a bit counterintuitive at first, but running Wasm on the server is much more reasonable than you might think. WASI, or WebAssembly System Interface, standardizes how Wasm interacts with the system and provides a modular environment for WebAssembly to do all its magic outside the browser, either on the desktop or in a server environment. With WASI, developers can write the same app for native and web without using multiple instances of Chrome with Electron.
In a way, this replaces much of what Docker accomplishes. The founder of Docker, Solomon Hykes, has even said that. Code written in any language (which distinguishes this from .NET / JVM) can probably be compiled for Wasm and run on any server with WASI. It removes the code from the machine (no more compiling for a specific CPU or operating system) and simply requires WASI to be compiled and installed on the machine it is running on. Although Docker will not go away either, and in the future it may be possible to run Linux, Windows and WASI containers side by side.
This is similar to the Java promise – the runtime environment will run the same code on a Windows host or a Linux host. The selling point for Wasm, however, is that you do not need to program in Java and are free to choose more performing languages such as C ++ and Rust.