Skip to content

Lesson 7 of 9 · 26m

The Performance Tradeoffs That Actually Matter

Boundary-crossing costs, batching, worker placement, SIMD and threads, startup cost, and how to benchmark Wasm against JavaScript honestly.

The worst Wasm performance work happens when someone benchmarks a fibonacci function, sees a 3x speedup, and ports half the application. Real systems are dominated by data movement, startup cost, and interaction shape. Wasm can be a decisive win, but only when the kernel is big enough to amortize the boundary.

Boundary crossings are the hidden constant

Calling an exported function is cheap compared to a network request, but not free compared to arithmetic. Copying a megabyte into memory, processing it, and copying it out can still win if the kernel is heavy. Calling the module once per pixel or once per token almost always loses.

Bad shape:

for (const sample of samples) {
  wasm.process_one(sample); // thousands of tiny crossings
}

Better shape:

const ptr = copySamplesIntoWasm(samples);
wasm.process_batch(ptr, samples.length);
const result = readResultBatch();

Benchmark the whole pipeline

A proper benchmark includes compile/instantiate time, marshalling, kernel execution, result extraction, and garbage-collection effects. Measure both cold and warm paths:

const t0 = performance.now();
const module = await WebAssembly.compileStreaming(fetch(url));
const t1 = performance.now();
// instantiate, marshal, execute, decode
const t2 = performance.now();
console.table({ compile: t1 - t0, total: t2 - t0 });

Compare against a typed-array JavaScript implementation, not the laziest object-heavy version you can write. Modern JITs are extremely good at monomorphic numeric loops.

Workers are usually the right home

Heavy kernels belong in Web Workers so compilation and computation don't block input. A typical design is:

main thread: UI, orchestration, transferable typed arrays
worker: wasm module + kernel execution

Transfer buffers with postMessage(buffer, [buffer]) rather than copying when ownership can move. If the same memory must be shared by true parallel workers, you enter SharedArrayBuffer territory — which requires cross-origin isolation headers such as Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp.

SIMD and threads are feature-dependent

Wasm SIMD can dramatically speed codecs and image work. Threads can help large parallel workloads but add operational constraints, non-determinism, and larger glue. Don't enable either because a benchmark blog did; profile whether your workload is vectorizable or embarrassingly parallel.

Field notes

  • A 10x kernel speedup can still be a product regression if marshalling costs 15x.
  • Reuse scratch buffers and instances in hot paths.
  • Wasm startup is often network-and-compile dominated; optimize size before optimizing the loop.
  • Always measure on low-end mobile hardware. Desktop benchmarks lie by omission.

Was this lesson helpful?

Sign in to rate