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

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:

Poor fits:

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

Best Practices

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

References