Wasm vs JavaScript

How We Measure

Measurement Methodology and Scopes

The same workload can take 0.08 ms as a warm in-memory loop and 35 ms as an end-to-end Web Worker task. This page explains where every timer starts and stops, why Worker Wasm can look slower than kernel compute, and how Track A parity stays separate from Track B optimizations.

1. Four Measurement Scopes

Every results table declares its scope. Ratios are only calculated inside the same table; the runner never divides a kernel median by a worker task or DOM journey time.

Where each timer starts and stops
Scope Timer Starts Timer Stops What It Counts
Delivery & First Use First new Worker() and postMessage() First verified result reaches the main thread Worker startup, network fetch(), decompression, SHA-256 checks, module compile/instantiate, first run, and verification.
Task Pipeline (Worker JS / Worker Wasm) Main thread calls worker.postMessage(cmd) Main thread receives worker.onmessage Structured-clone dispatch, any per-task asset load or instantiation inside the worker, fixture setup, memory copies, compute, and output verification.
Kernel Compute (Groups 1, 2, 3) Right before calling the exported JS or Wasm function Immediately after the function returns Pure CPU work and linear-memory reads/writes on an already-instantiated module with pre-allocated buffers. No worker IPC, network, or recompile.
Real-DOM Journey Host sends the frozen action trace to a live iframe Iframe finishes applying DOM updates and forced layout Engine compute, JS/Wasm bridge marshalling, real DOM API calls (createElement, insertBefore, classList, form state), and synchronous layout commit (getBoundingClientRect()), split into Compute, DOM Apply, and Layout phases.

2. Why Worker Wasm Can Look Slower Than Expected

On several pages, Worker Wasm takes 5 ms to 60 ms per task while the same algorithm in Kernel Compute takes 0.05 ms to 2 ms. That gap comes from worker lifecycle behavior, not from WebAssembly running slower inside a worker thread.

Is Wasm slower inside a Web Worker?

No. Dedicated Web Workers use the same V8 Liftoff and TurboFan compilers as the main thread. When a worker keeps a cached WebAssembly.Instance across messages, inner kernel speed matches main-thread Kernel Compute.

Is postMessage the bottleneck?

Usually not for small control messages. Sending a small command object and summary back across threads costs roughly 0.05-0.30 ms. Messaging only dominates when an app structured-clones megabytes of data per call instead of transferring ArrayBuffer ownership or keeping state inside the worker.

Why many contract workers show higher Worker Wasm time

JavaScript and WebAssembly are loaded differently in many of the self-attesting contract workers:

How to ship fast Worker Wasm in production

  1. Instantiate once per worker: Cache const wasmReady = WebAssembly.instantiateStreaming(fetch(url)) at module scope so onmessage reuses the same instance.
  2. Reuse a scratch arena: Keep linear-memory buffers allocated between calls instead of creating a new instance or growing memory per task.
  3. Batch small calls: Send a batch of frames, records, or edits per postMessage instead of crossing the thread boundary thousands of times.
  4. Transfer buffers zero-copy: Pass ArrayBuffers in the transfer list of postMessage, or share memory with SharedArrayBuffer under COOP/COEP.

3. Track A (Controlled) vs Track B (Optimized)

Inside Kernel Compute, each multi-language benchmark keeps baseline parity separate from target-specific tuning:

  1. Group 1 (JS vs Raw WAT): Compares plain JavaScript against handwritten WebAssembly Text (.wat) with no optimizing C or Rust compiler involved.
  2. Group 2 (Track A: Controlled Compilers): Runs the same algorithm and data layout in C (clang -O3), C++ (clang++ -O3), Rust (rustc -O), AssemblyScript (asc -O3), and Dart (dart compile wasm) without manual SIMD intrinsics or algorithm swaps.
  3. Group 3 (Track B: Tuned & SIMD128): Tests JavaScript loop/layout optimizations (js-opt), Wasm SIMD128 (-msimd128 -funroll-loops), loop tiling, and arena reuse. Each Track B variant is compared only against its own language's Track A baseline. Pre-built offline SIMD binaries under /artifacts/autoresearch/ (autoresearch-log.v1.json) were measured in standalone V8 CLI on Apple M4 Pro and are always tagged as non-browser reference numbers.

4. Four Workload Patterns

Across the suite, relative JS and Wasm performance falls into four clear patterns:

5. Distributions and Charts

6. Source Links and Output Verification

Every benchmark page links directly to its executed JS and Wasm source files and GitHub commits. Before timing runs, each target executes an output check (FNV-1a hash, SHA-256 digest, or exact DOM/state snapshot) and fails visibly if outputs disagree.