WebAssembly (Wasm)
WebAssembly is a portable, low-level bytecode format designed to run safely and at near-native speed alongside JavaScript. It's best understood not as a language you write directly, but as a compilation target: you write Rust, C/C++, Go, or others and compile to a .wasm module the browser (or a server runtime) executes.
Wasm fills JavaScript's performance gap for heavy computation while running in the same secure sandbox. You typically keep JavaScript/TypeScript for UI and orchestration and drop into Wasm for the hot paths — and with WASI, Wasm increasingly runs server-side and at the edge too.
TL;DR
- A portable bytecode compilation target for Rust, C/C++, and more.
- Great for performance-critical code paths; runs in a secure sandbox.
- You still use JS/TS for UI and orchestration — Wasm doesn't touch the DOM directly.
- Runs in browsers and server runtimes (via WASI).
Quick Example
JavaScript loads a Wasm module and calls its exported functions:
Real projects use toolchains like wasm-bindgen for richer JS ↔ Wasm interop (strings, objects).
When to Use WebAssembly
Good fits:
- Heavy computation — image/video processing, ML inference, simulation, cryptography.
- Performance-sensitive libraries — parsers, codecs, physics engines.
- Reusing existing native (C/C++/Rust) libraries on the web.
Poor fits:
- Simple CRUD apps — JavaScript is fine and simpler.
- DOM-heavy work — Wasm can't manipulate the DOM directly (it calls into JS).
Mental Model & Interop
Crossing the JS ↔ Wasm boundary has a cost, and Wasm has its own linear memory. Passing large data (or many small calls) across the boundary can erase the speedup — design for a few coarse calls, not chatty fine-grained ones.
Toolchains
- Rust → Wasm via
wasm-pack/wasm-bindgen(the most ergonomic path). - C/C++ → Wasm via Emscripten.
- Server/edge via WASI runtimes (Wasmtime, WasmEdge) and platforms like Fastly/Cloudflare.
Best Practices
- Use Wasm for CPU-bound hot paths, not whole apps.
- Minimize boundary crossings and large data copies between JS and Wasm.
- Keep UI and orchestration in JS/TS; expose a small, coarse-grained Wasm API.
- Profile to confirm Wasm is actually faster for your workload before adopting it.
Common Mistakes
Expecting Wasm to manipulate the DOM
Chatty boundary calls
FAQ
Is WebAssembly a replacement for JavaScript?
No — it complements it. JavaScript remains the language for UI, DOM manipulation, and orchestration; Wasm is for performance-critical computation. They interoperate, and most apps use Wasm only for specific hot paths, if at all.
What languages compile to WebAssembly?
Rust and C/C++ are the most common and mature (via wasm-bindgen/Emscripten); Go, C#, Zig, and others also target Wasm. Rust has the smoothest web tooling, which is why it's a popular pairing.
Does WebAssembly run outside the browser?
Yes. With WASI (the WebAssembly System Interface), Wasm runs in server and edge runtimes (Wasmtime, WasmEdge, Cloudflare/Fastly), offering fast cold starts and strong sandboxing — an emerging alternative to containers for some workloads.
Why might Wasm not be faster than JavaScript?
Modern JS JIT engines are very fast, and crossing the JS ↔ Wasm boundary (plus copying data into Wasm's linear memory) has overhead. For light or chatty work, that overhead can outweigh the gains. Wasm wins on sustained, CPU-heavy computation — measure to be sure.
Related Topics
- JavaScript — Orchestrates Wasm modules
- Rust — The most popular Wasm source language
- C/C++ — Compiled to Wasm via Emscripten
- Systems Programming — Where Wasm source languages live
- Web Performance — When Wasm helps
- WebAssembly Components — The component model and WASI