WebAssembly (Wasm) is a binary format that runs in the browser at near-native speed. It lets you take code written in languages like Rust, C, C++, or Go and run it in a web page.
JavaScript is great, but itβs slow for heavy computation β image processing, video editing, 3D games, encryption. WebAssembly fills that gap.
How it works
- You write code in Rust, C, C++, or Go
- You compile it to a
.wasmbinary file - Your JavaScript loads and runs the
.wasmfile in the browser - The browser executes it at near-native speed
// Load and run a WebAssembly module
const response = await fetch('module.wasm');
const { instance } = await WebAssembly.instantiateStreaming(response);
// Call a function defined in Rust/C
const result = instance.exports.fibonacci(40);
JavaScript vs WebAssembly
| JavaScript | WebAssembly | |
|---|---|---|
| Speed | Fast (JIT compiled) | Faster (pre-compiled) |
| Use case | UI, DOM, general logic | Heavy computation |
| Languages | JavaScript/TypeScript | Rust, C, C++, Go, etc. |
| DOM access | Direct | Through JavaScript |
| File size | Text (larger) | Binary (smaller) |
WebAssembly doesnβt replace JavaScript β they work together. JavaScript handles the UI and DOM, WebAssembly handles the heavy lifting.
Real-world examples
- Figma β the design tool runs C++ compiled to Wasm for performance
- Google Earth β 3D rendering in the browser
- Photoshop Web β Adobe ported parts of Photoshop to Wasm
- AutoCAD β runs in the browser via Wasm
- Game engines β Unity and Unreal can export to Wasm
- SQLite β runs entirely in the browser as Wasm
When to use WebAssembly
- β CPU-intensive tasks (image/video processing, compression)
- β Porting existing C/C++/Rust code to the browser
- β Games and 3D graphics
- β Cryptography and hashing
- β Scientific computing and simulations
- β Simple web apps (JavaScript is fine)
- β DOM manipulation (Wasm canβt access the DOM directly)
- β Small scripts (the overhead isnβt worth it)
Getting started with Rust + Wasm
Rust has the best WebAssembly tooling:
# Install the Wasm target
rustup target add wasm32-unknown-unknown
# Install wasm-pack (builds Rust β Wasm with JS bindings)
cargo install wasm-pack
# Create a new project
cargo new --lib my-wasm-project
// src/lib.rs
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn fibonacci(n: u32) -> u32 {
if n <= 1 { return n; }
fibonacci(n - 1) + fibonacci(n - 2)
}
wasm-pack build --target web
Beyond the browser
WebAssembly also runs outside the browser:
- Edge computing β Cloudflare Workers uses Wasm
- Serverless β Wasm containers start in milliseconds (vs seconds for Docker)
- Plugins β Envoy, Istio, and other tools use Wasm for extensibility