Wasm vs JavaScript — WebAssembly

Wasm is not automatically faster. On the 1200 × 800 Terrace Room photo, Wasm's grayscale computing takes 0.7 ms against JavaScript's 1.8 ms in Chromium 147…

If Wasm is bytes close to machine code, it should always beat JavaScript, right? Not quite. Sometimes it is faster, sometimes the same, sometimes even slower, depending on the work and on how you call it.

It's like Mrs. Sri sending the laundry from Mist Homestay to the laundry down the hill. A pickup truck (Wasm) carries a lot and drives fast, but takes time to load and unload. A motorbike (JavaScript) is already parked by the door and leaves at once. For one big sack, the truck wins. For a hundred round trips carrying one towel each, the motorbike wins, because every time the truck has to load and unload again.

The job: the Terrace Room photo in grey

A 1200 × 800 photo has 960,000 pixels. Each pixel is stored as 4 bytes: red, green, blue and alpha (RGBA), so 3,840,000 bytes in total. The grey formula uses integers so the JavaScript and Wasm results can be compared byte for byte:

// grey = (r × 77 + g × 150 + b × 29) >> 8, the weights add up to 256
function grayscaleJs(px) {
  for (let i = 0; i < px.length; i += 4) {
    const gray = (px[i] * 77 + px[i + 1] * 150 + px[i + 2] * 29) >> 8;
    px[i] = gray;
    px[i + 1] = gray;
    px[i + 2] = gray;
  }
}

The Wasm version (photo.wasm, 256 bytes) holds the same loop, plus two more functions: grayPixel(r, g, b) for one pixel, and totalBookings(n), a plain number loop to compare with. The photo bytes are first copied into Wasm memory, processed there, then copied out again.

How to measure honestly

function measure(fn, rounds = 10) {
  const times = [];
  for (let k = 0; k < rounds; k++) {
    const start = performance.now();
    fn();
    times.push(performance.now() - start);
  }
  const rest = times.slice(1).sort((a, b) => a - b);
  return { first: times[0], median: rest[Math.floor(rest.length / 2)] };
}

The results

Measured on a MacBook Pro M1 Pro that was busy running many other processes, the median of 9 rounds after the first, in milliseconds. On your device the numbers will differ; the Lab on this page measures again in your browser.

Job Node 22.22 (V8 12.4) Chromium 147 WebKit 26.4
Grayscale, JavaScript 1.4 1.8 1.8
Grayscale, Wasm (the computing only) 0.7 0.7 0.8
Copying the photo into and out of Wasm memory 0.5 0.6 0.6
Grayscale, Wasm called once per pixel 12 8 8
Loop over 20 million bookings, JavaScript 125 28 27
Loop over 20 million bookings, Wasm 18 18 17
Wasm loop, first round 44 45 18

The JavaScript and Wasm grayscale results are identical byte for byte, and so are the loop results (1107147376). The Firefox that could run on this laptop (Playwright's build of 146) only had the baseline Wasm compiler, so its numbers are left out: a regular Firefox has an optimising compiler too.

What the table tells you:

The cost of crossing

Every call from JavaScript into Wasm (and back) crosses a boundary: the arguments are converted to Wasm types, then the result is converted back. One crossing takes a few nanoseconds. Before you look at the table, guess: if grayPixel is called once per pixel (960,000 times), is that faster or slower than plain JavaScript?

How it is called Chromium 147
grayscale(0, 3840000), once for the whole photo 0.7 ms + 0.6 ms copying
grayPixel(r, g, b), once per pixel 8 ms

Per pixel turns out slower, about 4 times slower than plain JavaScript (8 ms against 1.8 ms). The rule: send the data in one go, call once, and let the loop run inside Wasm.

The cost of starting: download, compile, instantiate

Before the first function runs, the browser has to download the .wasm, compile it, then create an instance. For the 256-byte photo.wasm, compile plus instantiate takes only 0.06 to 0.3 ms. For a big module the cost moves around: V8 postpones compiling each function until the first time it is called (lazy compilation). A 1.3 MB test module with 4,000 functions in Node 22: compile() finishes in 3.6 ms, the first call to all its functions takes 44 ms, the second 1.5 ms.

For a file from a server, use instantiateStreaming. It compiles while the file is still downloading:

WebAssembly.instantiateStreaming(fetch('/wasm/photo.wasm')).then(({ instance }) => {
  const { grayscale } = instance.exports;
  // ready to use
});

It needs the server to send the header Content-Type: application/wasm. Otherwise Chrome 147 refuses: TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'. On a local server with no network delay, the difference from fetch, arrayBuffer() and then instantiate doesn't show (in Chromium 147, the 1.3 MB module: 12 ms against 9 ms, still noise). The gain only shows on a slow network.

When to use which

⚠️ Benchmarks that lead you to the wrong conclusion

TL;DR: Wasm is not automatically faster. On the 1200 × 800 Terrace Room photo, Wasm's grayscale computing takes 0.7 ms against JavaScript's 1.8 ms in Chromium 147, but copying the photo in and out adds 0.6 ms. Calling Wasm once per pixel (960,000 times) takes 8 ms, slower than JavaScript. A 20-million-step number loop: Wasm stays at 17 to 18 ms, JavaScript takes 28 ms in Chromium 147 but 125 ms in Node 22. Wasm has a warm-up too (44 ms, then 18 ms in V8) and a start-up cost: download, compile, instantiate; use instantiateStreaming with Content-Type: application/wasm. Measure with performance.now(), many rounds, the median, and results checked to be the same. Use Wasm for heavy computing whose data is sent in one go; JavaScript for the DOM, events and small functions, because Wasm cannot touch the DOM directly.

⚡ Measure honestly

Write:

function measure(fn, rounds = 10) {
  const times = [];
  for (let k = 0; k < rounds; k++) {
    const start = performance.now();
    fn();
    times.push(performance.now() - start);
  }
  const rest = times.slice(1).sort((a, b) => a - b);
  return { first: times[0], median: rest[Math.floor(rest.length / 2)] };
}

Meaning: The first round is kept apart, because in that round the engine is still compiling and optimising the code. From the remaining rounds, take the median, not a single number. Check too that the JavaScript and Wasm results are exactly the same.

⚡ The copying counts too

Meaning: The grayscale computing on the Terrace Room photo takes 0.7 ms in Wasm and 1.8 ms in JavaScript. But copying the photo into and out of Wasm memory costs 0.6 ms, so the total is 1.3 ms against 1.8 ms. A win, but a slim one.

⚡ Call once, not once per pixel

Guess first: if grayPixel(r, g, b) is called once per pixel, 960,000 times, is it faster than plain JavaScript?

It is slower: 8 ms against 1.8 ms in Chromium 147. Every call crosses the boundary from JavaScript into Wasm and back.

Meaning: Send the data in one go, call once, and let the loop run inside Wasm.

⚡ When to use Wasm

Meaning: Wasm suits heavy computing on big data that can be sent in one go: image processing, audio and video, compression, cryptography, game physics. The DOM, events, UI logic and small functions called often stay in JavaScript, because Wasm cannot access the DOM directly.

What you will learn