Overview

1 Meet WebAssembly

Modern web experiences live and die by performance, yet browsers historically ran only JavaScript, which is being pushed to do ever heavier work. In 2017, all major browsers shipped support for WebAssembly (Wasm), a compact, low-level, assembly-like format that executes at near‑native speed. Designed as a compilation target, WebAssembly lets code written in languages such as C, C++, and Rust run on the web, enabling reuse of existing codebases, powering new high-performance libraries, and opening a path to run beyond the browser as well.

WebAssembly addresses performance from download to execution. Whereas JavaScript is interpreted and JIT-compiled around dynamic types, Wasm is statically typed and compiled ahead of time to a binary bytecode that is fast to transmit, validate, and compile—benefiting startup on the very first run and further boosted by streaming compilation. It improves on asm.js (a typed subset of JavaScript) by avoiding large, parse-heavy JS files and by being a true compiler target. The workflow compiles source to an intermediate representation and then to .wasm bytecode; the browser validates it in a single pass and compiles it to machine code before instantiating the module. Modules expose a small set of numeric value types, manage their own linear memory, include well-defined known sections plus flexible custom sections, and also have a human-readable text form based on s-expressions for tooling and debugging.

Security comes from running inside the hardened, sandboxed JavaScript VM and adhering to the same policies (such as same-origin). Modules receive bounded linear memory, access function tables indirectly, and cannot touch the execution stack directly, limiting attack surface. WebAssembly complements rather than replaces JavaScript: today modules interoperate with JS (which can call Web APIs on their behalf), with direct host bindings planned. While C/C++ and Rust are first-class, many other languages are experimenting with Wasm targets. Support spans all major desktop and many mobile browsers, as well as Node.js, making it practical to accelerate performance-critical parts of applications, reuse existing native code, and deliver faster, more responsive web experiences.

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.

FAQ

What is WebAssembly (Wasm)?WebAssembly is a low-level, assembly-like binary format designed as a compilation target for languages such as C, C++, and Rust. It runs at near‑native speed in modern desktop and many mobile browsers. In 2017, Chrome, Edge, Firefox, Safari, and other major browsers shipped support for its MVP (Minimum Viable Product). Wasm modules are compact, quick to download, and fast to validate and initialize.
What problems does WebAssembly aim to solve?Primarily performance: faster downloads (compact binaries), faster parse/validation, and faster execution (ahead-of-time compilation from a statically typed format). It also enables using languages other than JavaScript in the browser and promotes code reuse by compiling existing native codebases to run on the web.
How does WebAssembly differ from JavaScript at runtime?JavaScript is interpreted and JIT-compiled from dynamic types, so engines monitor code to optimize hot paths. WebAssembly is statically typed and compiled ahead of time to a binary bytecode that browsers can rapidly validate and compile to native machine code immediately, yielding predictable, fast startup and execution. Wasm runs in the same VM as JavaScript and currently relies on JavaScript to access Web APIs.
What is asm.js and how did it lead to WebAssembly?asm.js is a typed, performance-oriented subset of JavaScript (triggered by "use asm") typically produced by transpiling C/C++ to JS. It improved performance via type hints and low-level operations but had drawbacks: large files from type hints, still parsed by the JS engine (costly on mobile), and adding features required changing JavaScript itself. WebAssembly keeps the performance wins while addressing these shortcomings with a compact binary and an independent evolution path.
How does WebAssembly achieve faster startup times?Wasm binaries are compact and structured for single-pass validation and parallel compilation. Because types are known up front, browsers can compile to machine code immediately without warm-up profiling. Streaming compilation further accelerates startup by compiling as the file downloads, enabling instantiation as soon as the download completes.
How are Wasm modules loaded, compiled, and instantiated?Today, JavaScript APIs fetch the .wasm binary, validate it, compile it to machine code, and instantiate it. Instantiation wires up imports, initializes module elements, optionally calls a start function, and returns an instance. Compiled modules can be transferred to Web Workers or other windows and used to create additional instances.
How is WebAssembly secured in the browser?Wasm shares the hardened, sandboxed JavaScript VM and inherits policies like same-origin. Modules have no direct system access; they operate within a bounds-checked linear memory (an ArrayBuffer) provided at initialization. Function references live in a separate table accessed indirectly by index, and the execution stack is separate from linear memory, preventing pointer-based stack tampering.
What is the structure of a Wasm module?A .wasm file begins with a preamble: magic number 0x00 61 73 6D ("\0asm") and version 0x01 00 00 00. It then contains optional sections. Known sections appear at most once in a required order and are validated on instantiation. Custom sections can appear anywhere, multiple times, and are not validated the same way (e.g., the post-MVP “name” section for debug symbols). Wasm currently supports four value types: i32, i64, f32, and f64; booleans use i32; other types (like strings) live in linear memory.
Which languages can target WebAssembly?Production tooling focuses on C and C++, with Rust also well supported. You can also author in the WebAssembly Text format (s-expressions) and compile to binary. Languages experimenting with Wasm include AssemblyScript (TypeScript → Wasm), TeaVM (Java), Go (with its own GC), Pyodide (Python with scientific stack), and Blazor (C#). Lack of built-in GC in the MVP constrains some languages, though GC is a post-MVP goal.
Where can I run WebAssembly?All major desktop browsers (Chrome, Edge, Firefox, Opera, Safari), several mobile browsers (e.g., Chrome, Firefox for Android, Safari), and Node.js (v8+) support Wasm. It was designed for portability, so usage extends beyond the browser as well. WebAssembly complements JavaScript—use each where it fits best, and interoperate within the same VM.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • WebAssembly in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • WebAssembly in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$399.99
only $33.33 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • WebAssembly in Action ebook for free