1 Meet WebAssembly
Modern web experiences demand speed and responsiveness, yet browsers historically ran only JavaScript, stretching it beyond its original intent for heavy computation and for codebases written in other languages. WebAssembly (Wasm) emerged—jointly supported by all major browsers—as a compact, low-level, assembly-like compile target that executes at near‑native speed. It complements rather than replaces JavaScript, runs inside the hardened JavaScript virtual machine, and is designed for portability so the same modules can run in browsers and beyond.
Wasm addresses performance end‑to‑end: its binary format downloads quickly, validates in a single pass, and can be compiled in parallel—and even streamed during download—for fast startup. Unlike dynamic, interpreted JavaScript that relies on profiling and JIT heuristics, Wasm’s static typing enables immediate, predictable compilation to machine code. Crucially, it lets developers bring C, C++, Rust, and other languages to the web without transpiling to JavaScript, unlocking code reuse, richer libraries, and acceleration of computational hot spots. It builds on lessons from asm.js—keeping the speed while shedding its bulk, parsing overhead, and language constraints.
Under the hood, source code is compiled to an intermediate representation and then to .wasm bytecode; the browser validates, compiles, and instantiates the module, which communicates with JavaScript and, for now, reaches Web APIs via JavaScript bridges. Security is central: modules run sandboxed in the existing VM, respect web security policies, operate within bounded linear memory, and access tables and stacks only through controlled mechanisms. The module format is well‑defined (preamble, known and custom sections, numeric value types), and a human‑readable text form based on s‑expressions aids viewing and debugging. With broad support across desktop and mobile browsers and in server environments like Node.js, WebAssembly opens the web to high‑performance code and a wider ecosystem of languages and libraries.
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