The Performance Question
WebAssembly promises near-native performance in the browser. But is it always faster than JavaScript? Let's find out with real benchmarks.
Test Setup
We'll compare three scenarios:
Benchmark 1: Fibonacci (CPU-bound)
// Rust
#[wasm_bindgen]
pub fn fibonacci(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
_ => fibonacci(n - 1) + fibonacci(n - 2)
}
}
// JavaScript
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
Results (fib(40), 100 iterations):
- JavaScript: 1,245ms
- Rust WASM: 892ms
- WASM wins by 28%
Benchmark 2: Array Processing
Processing 1M elements with map/reduce operations. Results:
- JavaScript: 45ms
- Rust WASM: 52ms (with copy overhead)
- Rust WASM SharedArrayBuffer: 23ms
- WASM wins only with shared memory
When to Use WASM
Use WASM for:- Heavy computation (image processing, cryptography)
- Games and simulations
- Porting existing C/C++/Rust codebases
- DOM manipulation
- Light data processing
- When bundle size matters
Conclusion
WASM isn't a silver bullet. The overhead of crossing the JS-WASM boundary can negate performance gains for small operations. Profile first, optimize second.