WebAssembly

Also known as: Wasm

On this page 7

WebAssembly, usually shortened to Wasm, is a portable binary instruction format for a stack-based virtual machine that browsers execute at near-native speed inside the same sandbox as JavaScript. Among the Web APIs, it is the one that lets code written in languages such as C, C++, Rust or Go be compiled once and run in any modern browser without plugins.

WebAssembly does not replace JavaScript. It is a compilation target, not a language people normally write by hand, and it depends on JavaScript and other Web APIs for almost everything outside pure computation, including access to the page, the network and the graphics stack.

How WebAssembly runs in the browser

A WebAssembly program arrives as a module, a .wasm file containing typed functions, a linear memory definition, tables and lists of imports and exports. The browser validates the module before running it, which is fast because the format was designed for single-pass checking of types and control flow.

After validation, the engine compiles the bytecode to machine code. Most engines use a tiered strategy: a quick baseline compiler produces runnable code within milliseconds, and an optimizing compiler replaces hot functions later. With WebAssembly.instantiateStreaming(), compilation can start while the file is still downloading, which shortens startup for large modules.

Execution uses a linear memory, a contiguous, resizable array of bytes that the module reads and writes directly. Every access is bounds-checked, so a module cannot touch memory outside its own buffer. JavaScript sees that same memory as an ArrayBuffer, which is how the two sides exchange strings, images and other bulk data.

The table below lists the main building blocks of a module and the role each one plays at run time.

ComponentWhat it containsWhy it matters
FunctionsTyped instructions for a stack machineCarry the compiled program logic
Linear memoryResizable byte array in 64 KiB pagesHolds heap data shared with JavaScript
TablesArrays of function referencesEnable indirect calls and function pointers
ImportsFunctions and memory supplied by the hostGive access to Web APIs and the page
ExportsFunctions and values offered to the hostLet JavaScript call into the module
Core parts of a WebAssembly module

Key features and proposals of WebAssembly

The WebAssembly 1.0 specification, standardized by the W3C in 2019, defined a deliberately small core: four numeric types (i32, i64, f32, f64), structured control flow and one linear memory. Later work has been added through feature proposals, each moving through phases before browsers ship it.

The following table summarizes widely deployed extensions and what they enable. Support differs between browser versions, so production code usually performs feature detection before relying on any of them.

FeatureWhat it addsTypical benefit
SIMD128-bit vector instructionsFaster image, audio and math kernels
ThreadsShared memory and atomic operationsParallel work across Web Workers
Bulk memoryFast copy and fill operationsQuicker data movement inside memory
Reference typesOpaque references to host objectsCleaner interaction with JavaScript values
Garbage collectionManaged structs and arraysEfficient targets for Kotlin, Dart and Java
Exception handlingNative throw and catchSmaller code for C++ and managed languages
Selected WebAssembly extensions and their effect

Threads depend on SharedArrayBuffer, which browsers only enable on pages that are cross-origin isolated through the COOP and COEP response headers. Teams that skip this step find that threaded builds silently fall back or fail to load.

WebAssembly compared with JavaScript and graphics APIs

WebAssembly is best understood as a complement to JavaScript, not a competitor. JavaScript remains the language of the page and the DOM, while Wasm takes over CPU-intensive, predictable workloads. It is also distinct from WebGL and WebGPU, which run work on the graphics processor rather than the CPU.

The comparison below shows where each technology fits in a typical web application.

TechnologyRuns onStrengthMain limitation
WebAssemblyCPU, sandboxed VMPredictable speed, reuse of native codeNo direct DOM access
JavaScriptCPU, JIT engineFull access to the page and Web APIsLess predictable performance
WebGLGPUMature 2D and 3D renderingLimited general computation
WebGPUGPUModern rendering and compute shadersNewer, narrower device support
Where WebAssembly fits among related technologies

A common misconception is that Wasm is always faster than JavaScript. Modern JavaScript engines are highly optimized, and for short tasks or DOM-heavy code the cost of crossing the boundary between the two can erase any gain. Wasm wins most clearly in long-running numeric loops, codecs, compression, cryptography and physics.

Building and shipping a WebAssembly module

In practice, developers write code in a source language and use a toolchain to produce the .wasm file plus JavaScript glue code. Emscripten is the standard route for C and C++, Rust uses wasm-pack and wasm-bindgen, and Go, Zig and AssemblyScript have their own compilers. The human-readable text format (WAT) exists mainly for debugging and teaching.

The decision table below helps teams judge whether WebAssembly is a sensible choice for a given workload.

SituationRecommendationReason
Existing C++ or Rust libraryUse WasmReuses proven code without a rewrite
Heavy image or video processingUse Wasm with SIMDVector instructions speed up pixel loops
Mostly DOM updates and formsStay with JavaScriptBoundary calls would add overhead
Small utility functionsStay with JavaScriptDownload and startup cost outweighs gains
Parallel scientific computationUse Wasm with threadsShared memory scales across cores
When WebAssembly is a good fit

Delivery details matter. Servers must send .wasm files with the application/wasm MIME type for streaming compilation to work, files should be compressed with Brotli or gzip, and long-lived HTTP caching prevents users from downloading the same module repeatedly.

Limitations and security of WebAssembly

WebAssembly inherits the same-origin policy and the browser security model, so a module has no more privileges than the JavaScript that loads it. It cannot open files, sockets or windows on its own; every capability must be passed in as an import.

  • No direct DOM access: page changes still go through JavaScript, which adds glue code and call overhead.
  • Binary size: careless builds that include a full runtime or standard library can reach several megabytes.
  • Memory safety inside the module: bounds checks protect the browser, but a C++ buffer overflow can still corrupt the module’s own data.
  • Debugging: source maps and DWARF support exist, yet stepping through optimized Wasm remains harder than debugging JavaScript.
  • Abuse: Wasm has been used for in-browser cryptocurrency mining, which is why some security tools monitor unusual CPU use by modules.

WebAssembly has also spread beyond the browser. The WebAssembly System Interface (WASI) defines access to files, clocks and sockets for server and edge runtimes, but that use lies outside the browser-focused Web APIs discussed here.

In simple terms

WebAssembly is a way to run fast, pre-built programs inside a web page. Normally a browser runs JavaScript, which it reads and interprets as it goes. WebAssembly instead delivers code that has already been translated into a compact, machine-friendly form, so the browser can run it almost as quickly as an installed application.

Think of JavaScript as a skilled manager who talks to everyone on the page, and WebAssembly as a specialist who does heavy calculations in a locked workshop. The manager hands over the raw material, the specialist works quickly, and the result comes back. The key takeaway is that WebAssembly makes demanding tasks possible in the browser while staying just as safely contained as ordinary web code.

A practical example

A small company builds a browser-based photo editor. Its first version applies filters in JavaScript, and a sharpening filter on a 24-megapixel image takes about four seconds, long enough for users to think the page has frozen.

The team already owns a well-tested C++ image library from a desktop product. They compile it with Emscripten, enable SIMD instructions, and expose three functions to JavaScript: load pixels, apply filter, return pixels. JavaScript copies the image into the module’s linear memory, calls the filter and draws the result onto a canvas. They serve the 900 KB module with Brotli compression and the correct MIME type.

The same filter now completes in well under one second on a typical laptop, and the team maintains a single codebase for desktop and web. The user interface stays in JavaScript, which keeps the boundary narrow and the gain real.