Skip to main content

Browser Sandbox · WebAssembly · Multi-Core HPC

Algorithm Benchmark Lab

Measure real execution times right inside your browser using isolated Web Workers (no server load, 30-second watchdog timeout) and compare JavaScript vs. WebAssembly and multi-core speedup vs. Amdahl’s Law.

1. Isolated Sandbox: JavaScript vs. WebAssembly (WASM)

Executes naive recursive fib(n) (exponential O(φn) workload, up to n = 46, the largest Fibonacci number that fits in a signed 32-bit integer) inside an isolated same-origin Web Worker with a 30 s watchdog, comparing JavaScript against an embedded 60-byte .wasm binary. Chapter 1 →

What do these concepts mean? (O(φⁿ), Web Worker sandbox, Watchdog, WASM)
  • O(φn) (exponential workload): φ = (1 + √5) / 2 ≈ 1.618 is the golden ratio. Naive recursive Fibonacci recomputes the same subproblems over and over, so each increment of n multiplies the work by ~1.618× (going from n = 35 to n = 45 makes it ~123× slower).
  • Isolated Web Worker (sandbox): a separate background thread loaded from a static same-origin script (/workers/benchmark-worker.js) with no access to the DOM, cookies, or localStorage, keeping the interface responsive while heavy computations run.
  • Watchdog timeout (30 s): a safety timer on the main thread that forcefully kills (worker.terminate()) any execution exceeding 30 seconds so a runaway job can never freeze the browser.
  • WebAssembly (.wasm): a compact binary instruction format (here just 60 bytes) compiled directly to native machine code by the browser without JavaScript’s dynamic-typing overhead.
  • WASM Speedup (TJS / TWASM): how many times faster WebAssembly ran than JavaScript on the exact same recursive algorithm (> 1.0× means WASM is faster).

2. Multi-Core Parallelism (HPC) vs. Amdahl’s Law

Counts prime numbers up to N in parallel across 1..4 concurrent Web Workers (detected 4 logical CPU cores on this device) and compares empirical speedup S(p) and efficiency E(p) with Amdahl’s Law. Chapter 9 →

What do these metrics mean? (Workers p, Speedup S(p), Efficiency E(p), Amdahl’s Law)
  • Workers (p): number of concurrent Web Workers running in parallel, each testing a disjoint sub-range of [2..N] across your CPU’s logical cores (navigator.hardwareConcurrency).
  • Wall Time (T(p)): total elapsed time in milliseconds from launching all p workers until the slowest worker finishes.
  • Measured Speedup S(p) = T(1) / T(p): how many times faster p workers ran compared to 1 worker. Ideal linear scaling would be S(p) = p (e.g., 4× faster on 4 cores).
  • Efficiency E(p) = (S(p) / p) × 100%: fraction of theoretical linear speedup actually achieved per core (100% = perfect scaling; lower values reflect worker startup, message-passing overhead, and the fact that larger numbers take longer to test for primality).
  • Amdahl’s Law SAmdahl(p) = 1 / (s + (1 − s) / p): theoretical ceiling on speedup when a fraction s of the program is strictly sequential. Even with infinite cores (p → ∞), speedup can never exceed 1 / s (e.g., s = 10% caps speedup at 10×).