1 Meet WebAssembly
Modern web development is shaped by performance expectations and the constraints of browsers historically running only JavaScript. As sites grew into full applications and workloads like games, simulations, and data processing pushed beyond JavaScript’s original design, developers needed a faster, more portable path to the web. WebAssembly (Wasm), adopted by all major browsers in 2017, answers this need: it’s a compact, low-level, statically typed, binary format that executes at near‑native speed and is designed as a compilation target for languages such as C, C++, and Rust. This enables code reuse, opens the web to a broader range of languages and libraries, and extends beyond browsers to other runtimes as well.
Wasm tackles performance end to end—from download size to execution speed. Unlike JavaScript’s interpreted and JIT-compiled model, with dynamic types that require runtime monitoring, Wasm’s static types allow immediate compilation to machine code. Its binary format validates in a single pass, supports parallel compilation, and even supports streaming compilation as bytes arrive, yielding fast startup. It improves on asm.js, which pioneered many ideas but suffered from large text payloads, parsing overhead, and language limitations. In practice, developers compile source code to an intermediate representation, emit Wasm bytecode, and let the browser compile it to the device’s machine code on load.
Wasm runs alongside JavaScript in the hardened, sandboxed JavaScript virtual machine, complementing rather than replacing JavaScript. Modules are validated, compiled, and instantiated with explicit imports, linear memory is bounds-checked, indirect function calls go through tables, and the execution stack is isolated—forming a robust security model. The module format is structured into known and custom sections, exposes a minimal set of numeric value types, and has a human-readable text form based on s-expressions for debugging and “view source.” With initial emphasis on C/C++ and growing support for Rust and other languages, Wasm is widely available across major desktop and mobile browsers and in environments like Node.js, enabling faster components, new libraries, and practical code reuse across web and non-web platforms.
JavaScript compiled to machine code as it executes
C++ being turned into WebAssembly and then into machine code in the browser
Compiler front-end and back-end diagram
Compiler front-end with a WebAssembly back-end diagram
Wasm file loaded into a browser and then compiled to machine code diagram
Summary
As you saw in this chapter, WebAssembly brings a number of improvements with many being around performance as well as language choice and code reuse. Some of the key improvements that WebAssembly brings are the following:
- Faster transmission and download times because of the smaller file sizes due to the use of a binary encoding.
- Due to the way the files are structured, they can be parsed and validated quickly. Also because of how they’re structured, portions of the file can be compiled in parallel.
- With streaming compilation, WebAssembly modules can be compiled as they’re being downloaded so that they’re ready to be instantiated the moment the download completes speeding up load time considerably.
- Faster code execution for things like computations due to the use of machine level calls rather than the more expensive JavaScript engine calls.
- Code doesn’t need to be monitored before it’s compiled to determine how it’s going to behave. The result is that code runs at the same speed every time it runs.
- Being separate from JavaScript, improvements can be made to WebAssembly faster because it won’t impact the JavaScript language.
- The ability to use code written in a language, other than JavaScript, in a browser.
- Increased opportunity for code reuse by structuring the WebAssembly framework in such a way that it can be used in the browser and outside of the browser.
WebAssembly in Action ebook for free