← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

WebAssembly (Wasm)

frontend Advanced

TL;DR

A binary instruction format that runs at near-native speed in the browser and on servers — enabling C, Rust, and Go code to run alongside JavaScript without plugins.

Explanation

WebAssembly is a low-level bytecode format designed as a compilation target for languages like C, C++, Rust, and Go. Browsers execute Wasm at near-native speed via a sandboxed virtual machine alongside the JS engine. Wasm modules can be instantiated from JavaScript, share memory via a linear memory buffer, and call JS functions as imports. On the server side, WASI (WebAssembly System Interface) provides a portable, sandboxed runtime model used in edge computing (Cloudflare Workers, Fastly Compute). PHP can run compiled to Wasm via the php-wasm project, enabling PHP execution in the browser. Key use cases: CPU-intensive computations (image processing, codecs, compression), porting existing native libraries to the web without rewriting, and untrusted code sandboxing. Wasm does not replace JavaScript for DOM manipulation — it cannot access the DOM directly and must call JS for any browser API interaction.

Watch Out

Every call across the JS/Wasm boundary has overhead — an inner loop that calls a Wasm function on each iteration can be slower than pure JS. Move the entire loop into Wasm and call it once.

Common Misconception

WebAssembly does not replace JavaScript — it cannot access the DOM directly and must go through JavaScript bridge calls for any browser API interaction.

Why It Matters

Wasm unlocks CPU-intensive tasks in the browser (video processing, compression, game engines) that would be impractical in JavaScript, and enables reuse of battle-tested native libraries without rewriting them.

Common Mistakes

  • Reaching for Wasm to speed up I/O-bound JS code — Wasm only helps CPU-bound work; network and DOM operations are no faster.
  • Ignoring the JS/Wasm bridge cost — frequent small calls between JS and Wasm via imports/exports add significant overhead; batch operations.
  • Shipping unoptimised Wasm binaries — without wasm-opt or LTO, compiled output can be significantly larger and slower than necessary.

Avoid When

  • Do not use Wasm for DOM manipulation, network requests, or anything that must call browser APIs — JS is faster for those due to bridge overhead.
  • Avoid Wasm when the compiled binary size outweighs the performance gain for your use case — a 2 MB Wasm module adds to initial load time.
  • Do not reach for Wasm before profiling — most web performance bottlenecks are I/O or rendering, not CPU computation.

When To Use

  • Use Wasm for CPU-bound browser tasks: image/video processing, audio codecs, compression, cryptography, simulation.
  • Use it to port existing C/C++/Rust libraries to the browser without rewriting them in JavaScript.
  • Consider Wasm for sandboxed execution of untrusted code — the Wasm VM provides strong isolation by default.

Code Examples

💡 Note
The bad example makes a Wasm call per pixel — millions of JS→Wasm crossings dominate the runtime. The fix copies the entire pixel buffer into Wasm linear memory and processes it in a single call, reducing bridge crossings to three.
✗ Vulnerable
// Calling Wasm per-pixel — bridge overhead dominates:
const wasmProcess = instance.exports.processPixel;
for (let i = 0; i < pixels.length; i += 4) {
    pixels[i] = wasmProcess(pixels[i]); // JS→Wasm per pixel
}
✓ Fixed
// Pass entire buffer to Wasm — one bridge call:
const ptr = instance.exports.alloc(pixels.length);
new Uint8Array(instance.exports.memory.buffer, ptr, pixels.length).set(pixels);
instance.exports.processImage(ptr, pixels.length); // single call
const result = new Uint8Array(instance.exports.memory.buffer, ptr, pixels.length);

Added 31 Mar 2026
Views 12
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Perplexity 3 Google 2 ChatGPT 1 Ahrefs 1
crawler 6 crawler_json 1

✓ schema.org compliant