Overview

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.

FAQ

What is WebAssembly (Wasm)?WebAssembly is a low-level, assembly-like, statically typed bytecode that runs at near-native speeds in all modern desktop browsers and many mobile browsers. It’s compact, quick to download and initialize, and is designed as a compilation target so code written in languages like C++ and Rust can run on the web and beyond.
What problems does WebAssembly solve?Wasm primarily tackles performance: faster downloads (compact binary), faster parsing/validation, and faster execution. It also lets developers use languages other than JavaScript and reuse existing native code on the web.
How does WebAssembly achieve faster startup and execution than JavaScript?Unlike dynamic, interpreted JavaScript, Wasm is statically typed and compiled. Browsers can validate it in a single pass, compile sections in parallel, and even compile while downloading (streaming compilation), delivering first-run performance without monitoring warm-up.
What is asm.js and how did it influence WebAssembly?asm.js is a typed subset of JavaScript, typically generated from C/C++, that enabled faster execution via type hints and a “use asm” pragma. Its shortcomings—large files, JS engine parsing costs, and limited extensibility—led browser vendors to create Wasm, which keeps the speed benefits while fixing those issues.
How does source code become a running WebAssembly module in the browser?Code (e.g., C/C++) is compiled to an intermediate representation (IR), then to Wasm bytecode (.wasm). In the browser, the module is validated and compiled to the device’s machine code. The binary format enables single-pass validation and parallel/streaming compilation.
How are Wasm modules loaded and instantiated today?Currently via JavaScript APIs: fetch the .wasm bytes, validate, compile, and instantiate with required imports. Instantiation initializes the module, runs an optional start function, and returns an instance. Compiled modules can be shared with Web Workers or other windows and used to create multiple instances.
How do WebAssembly modules interact with JavaScript and Web APIs?Wasm runs in the same VM as JavaScript and complements it. Today, modules call into JavaScript to use Web APIs; direct access is planned via Host Bindings. Use Wasm for compute-heavy or low-level tasks (e.g., SIMD), and JavaScript for the rest.
What makes WebAssembly secure?Wasm shares the hardened, sandboxed JavaScript VM and follows the same security policies (e.g., same-origin). Modules use bounded linear memory via an ArrayBuffer, access tables indirectly by index, and cannot access the execution stack or host memory directly.
What is the structure of a Wasm module?A .wasm file starts with a magic number and version, followed by optional sections. Known sections appear once in a fixed order and are validated; Custom sections can appear anywhere and multiple times. A standard “name” Custom section holds debug names.
Which languages can target Wasm, and where can modules run?C and C++ were the initial focus; Rust also supports Wasm, and a text format (WAT) exists. Experiments include AssemblyScript, TeaVM (Java), Go, Pyodide (Python), and Blazor (C#). Wasm runs in Chrome, Edge, Firefox, Opera, Safari (desktop), several mobile browsers, and Node.js (v8+).

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