For two decades now, we’ve had only one programming language available to use natively in a web browser: JavaScript. The slow death of third-party binary plug-ins has ruled out other languages, such as Java and Flash’s ActionScript, as first-class citizens for web development. Other web languages, like CoffeeScript, are merely compiled to JavaScript.
But now we have a new possibility: WebAssembly, or WASM for short. WebAssembly is a small, fast binary format that promises near-native performance for web applications. Plus, WebAssembly is designed to be a compilation target for any language, JavaScript being just one of them. With every major browser now supporting WebAssembly, it’s time to start thinking seriously about writing client-side apps for the web that can be compiled as WebAssembly.
It is worth noting that WebAssembly apps aren’t meant to replace JavaScript apps—at least, not yet. Instead, think of WebAssembly as a companion to JavaScript. Where JavaScript is flexible, dynamically typed, and delivered through human-readable source code, WebAssembly is high-speed, strongly typed, and delivered via a compact binary format.
Developers should consider WebAssembly for performance-intensive use cases such as games, music streaming, video editing, and CAD applications.
How WebAssembly works
WebAssembly, developed by the W3C, is in the words of its creators a “compilation target.” Developers don’t write WebAssembly directly; they write in the language of their choice, which is then compiled into WebAssembly bytecode. The bytecode is then run on the client—typically in a web browser—where it’s translated into native machine code and executed at high speed.
WebAssembly code is meant to be faster to load, parse, and execute than JavaScript. When WebAssembly is used by a web browser, there is still the overhead of downloading the WASM module and setting it up, but all other things being equal WebAssembly runs faster. WebAssembly also provides a sandboxed execution model, based on the same security models that exist for JavaScript now.
Right now, running WebAssembly in web browsers is the most common use case, but WebAssembly is intended to be more than a web-based solution. Eventually, as the WebAssembly spec shapes up and more features land in it, it may become useful in mobile apps, desktop apps, servers, and other execution environments.