WebAssembly
In short
WebAssembly is a compact binary code format that runs in web browsers at near-native speed, letting languages like C++, Rust, and Go power fast web code.
What is WebAssembly?
WebAssembly, usually shortened to Wasm, is a low-level instruction format that browsers can run alongside JavaScript. Developers rarely write it by hand; instead they compile code written in languages such as C, C++, Rust, Go, or C# into a .wasm file, which the browser downloads, validates, and turns into fast machine code.
Wasm is designed for work that is too heavy for JavaScript alone, such as image and video editing, 3D games, design and CAD tools, audio processing, scientific computing, and running AI models in the browser. It also lets teams reuse existing libraries written in other languages on the web instead of rewriting them in JavaScript.
A Wasm module runs in a sandbox with no direct access to the page, the network, or files; it can only use what JavaScript explicitly passes to it. JavaScript loads the module, calls its exported functions, and handles the DOM, so the two work as partners. Think of JavaScript as the manager who talks to customers and WebAssembly as the specialist in the back room who does the heavy calculations.
A common misconception is that WebAssembly is meant to replace JavaScript. It is a complement: most web UI code is still written in JavaScript or TypeScript, and Wasm handles the performance-critical parts. WebAssembly also runs outside browsers, on servers, edge platforms, and plugin systems, where the WebAssembly System Interface (WASI) gives it controlled access to files and other system resources.
Key takeaways
- WebAssembly (Wasm) is a binary format that runs at near-native speed.
- Code in languages like C++, Rust, and Go can be compiled to Wasm.
- It runs in a secure sandbox and works alongside JavaScript.
- It suits heavy tasks like games, media editing, and in-browser AI.
- WASI lets Wasm run outside the browser, on servers and at the edge.
Example
// Load a compiled module (for example, one written in Rust or C++)
const { instance } = await WebAssembly.instantiateStreaming(
fetch("/math.wasm")
);
// Call a function exported by the module like a normal JS function
const result = instance.exports.add(2, 3);
console.log(result); // 5Readers ask
Will WebAssembly replace JavaScript?
No. WebAssembly is designed to work alongside JavaScript, not replace it. JavaScript still handles most UI work and access to the DOM, while Wasm speeds up computation-heavy parts.
Is WebAssembly faster than JavaScript?
For CPU-heavy work such as number crunching, image processing, or physics, WebAssembly is usually faster and more predictable. For typical UI code, modern JavaScript engines are already fast, and passing data back and forth between the two has its own cost.
Can WebAssembly access the DOM?
Not directly. A Wasm module can only call functions that JavaScript provides to it, so DOM updates go through JavaScript, often via glue code generated by the compiler toolchain.
See also
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- CompilerProgramming Fundamentals, p. 11A compiler is a program that translates source code written in a programming language into a lower-level form, such as machine code, that a computer can run.
- DOMWeb Development, p. 13The DOM is the browser's in-memory tree of objects representing a web page, which JavaScript can read and change to update what the user sees.
- TypeScriptWeb Development, p. 50TypeScript is a programming language built on JavaScript that adds static types, catching many bugs before the code runs, and compiles to plain JavaScript.
- ServerlessDevOps & Cloud, p. 47Serverless is a cloud model in which the provider runs your code on demand, manages all the servers, scales automatically, and bills only for actual use.
- JIT CompilationProgramming Fundamentals, p. 32JIT compilation turns the busiest parts of a program into machine code while it runs, combining an interpreter's quick start with compiled speed.
- Web WorkerWeb Development, p. 58A web worker is a browser feature that runs JavaScript on a background thread, so heavy computations don't freeze the page's user interface while they run.
Spotted a mistake or something missing on this page?Suggest an edit