What Exactly Is WebAssembly?
Because Wasm was designed from the ground up, it’s built with performance in mind. It strips away the complex prototyping from JS, and introduces proper primitives. JS only has floats for numbers—every math operation in JS is done using floats, even though float math is much slower and unnecessary in most cases. Wasm provides primitives for ints, and provides byte-level access to memory. It has a textual representation called WebAssembly Text (
.wat), which you can use to code in directly. You’ll always be packaging it up in its binary format for use on the web. Under the hood, Wasm runs on a stack-based virtual machine, which translates the instructions to actual code that runs on your machine.
Because it’s just an instruction format, the magic of Wasm is that it can be used as a portable compilation target for other languages, without sacrificing on speed like you would have with transpiling. This includes languages like C++ and Rust, which can now run on the web when compiled to Wasm.
If your mind was just blown, this isn’t theoretical—it actually works in practice. AutoDesk was able to port AutoCAD, a 30-year-old C++ codebase, over to a WebAssembly-based web app, in a few months, with relative ease. Native apps being ported to the web is a major-use case for Wasm, and we’ll start seeing more apps taking this approach as Wasm pushes what’s possible in the browser.
If you want to get started working with Wasm, you can head over to an online WebAssembly playground to try it out.
JS is also much more accessible, as proven by the success of client-side JS frameworks like React. Web apps built with React are still quite fast, and with good render optimization can run at 60 FPS without jank on most smartphones.
Wasm on the Desktop
This seems a bit counterintuitive at first, but Wasm running on the server is much more reasonable than you would think. WASI, or WebAssembly System Interface, standardizes how Wasm interacts with the system, and provides a modular environment for WebAssembly to 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, without resorting to running more instances of Chrome with Electron.
In a sense, this replaces much of what Docker accomplishes. The founder of Docker, Solomon Hykes, has even said so. Code written in any language (which makes this different from .NET/JVM) can probably compile to Wasm, and be ran on any server using WASI. It abstracts away the code from the machine (no more compiling for a specific CPU or OS), and simply requires WASI to be compiled and installed on the machine it runs on. Though Docker isn’t going away either, and in the future it may be able to run Linux, Windows, and WASI containers side-by-side.
This is similar to the promise made by Java—the runtime environment will run the same code on a Windows host or a Linux host. The selling point of Wasm, however, is that you don’t have to program in Java, and are free to choose more performant languages such as C++ and Rust.