Overview

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.

FAQ

What is WebAssembly (Wasm)?WebAssembly is a low-level, assembly-like, statically typed, portable binary format designed as a compilation target. It runs at near-native speed in modern desktop and many mobile browsers, with initial MVP support shipped by all major vendors in 2017.
What problems does WebAssembly solve for the web?It addresses performance end to end (smaller downloads, faster parsing/validation, quicker execution), enables code written in languages like C/C++/Rust to run on the web without transpiling to JavaScript, and promotes code reuse across platforms. It also decouples low-level performance features from JavaScript’s language design.
How does WebAssembly compare to JavaScript and asm.js?JavaScript is interpreted/JIT-compiled and dynamically typed, so engines must observe code before optimizing it. asm.js was a typed subset of JavaScript that improved speed but produced large files and still required JavaScript parsing. WebAssembly uses a compact binary format with static types, enabling rapid validation and compilation without changing JavaScript itself. It complements, not replaces, JavaScript.
Why does WebAssembly start up and run faster?Wasm modules are compact binaries that validate in a single pass and support parallel compilation. Because types are known ahead of time, engines can compile to machine code immediately. Browsers also implement streaming compilation, compiling as bytes arrive over the network, which further reduces startup time.
How does the WebAssembly toolchain work under the hood?Source code (e.g., C/C++) is compiled by a front end to an intermediate representation (IR). A WebAssembly back end emits .wasm bytecode. In the browser, the .wasm is validated and then compiled to the device’s machine code at load time.
How are Wasm modules loaded and instantiated in the browser?Today, JavaScript APIs fetch the .wasm file, validate it, compile it, and then instantiate it by wiring imports, initializing module elements, optionally calling a start function, and returning an instance. Compiled modules can be reused or transferred to Web Workers. Wasm currently reaches Web APIs indirectly by calling into JavaScript; direct bindings are a post-MVP goal.
What is inside a WebAssembly module?A module begins with a magic number and version (the preamble), followed by optional sections. Known sections appear once in a fixed order and are validated; Custom sections can appear anywhere and are for auxiliary data (e.g., debug names). Modules expose functions, linear memory, and tables, and use four numeric value types: 32/64-bit integers and 32/64-bit floats. Other data (e.g., strings) live in linear memory.
What is the WebAssembly Text Format (WAT) and why is it useful?WAT is a human-readable s-expression syntax that mirrors the binary format. It enables view-source and debugging, and you can assemble WAT into .wasm (and disassemble .wasm back to WAT) for inspection and learning.
How is WebAssembly kept secure?Wasm runs inside the hardened, sandboxed JavaScript VM and follows the same browser security policies (such as same-origin). Modules use bounds-checked linear memory (an ArrayBuffer), access function references indirectly through tables, and cannot access the execution stack or host memory directly.
Which languages and environments support WebAssembly today?C and C++ (initial focus) and Rust have mature support. Other efforts include AssemblyScript (TypeScript), TeaVM (Java), Go’s WebAssembly target, Pyodide (Python stack), and Blazor (C#). Wasm runs in all major desktop browsers, several mobile browsers, and outside the browser in Node.js.

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
$499.99
only $41.67 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
$499.99
only $41.67 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
$499.99
only $41.67 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