Skip to content
WebAssembly

Performance

Benchmark and parallelize WASM workloads.

#performance#benchmark#threads

Code

webassembly
// Bench: WASM vs JS for a tight numeric loop
function jsSum(n) {
  let total = 0;
  for (let i = 0; i < n; i++) total += i;
  return total;
}

const { sum: wasmSum } = instance.exports;
const N = 100_000_000;

console.time("js");
jsSum(N);
console.timeEnd("js");

console.time("wasm");
wasmSum(N);
console.timeEnd("wasm");

// Threads via SharedArrayBuffer (requires COOP/COEP headers)
// const shared = new WebAssembly.Memory({
//   initial: 1, maximum: 10, shared: true,
// });