Metrics
numThreads

numThreads

numThreads returns an integer (1 to N) representing the exact maximum number of Web Workers that can execute in parallel on physical CPU cores.

What It Measures

Modern web applications rely on Web Workers and multi-threaded WebAssembly (SharedArrayBuffer / Wasm Threads) to run parallel workloads off the Main Thread. Historically, developers relied on navigator.hardwareConcurrency to size worker pools—an approach that breaks down in production:

  1. Browser Spoofing & Caps: Safari and privacy-hardened browsers artificially cap navigator.hardwareConcurrency (frequently reporting 2 or 4 even on 8-core or 12-core chips) to mitigate device fingerprinting.
  2. Thread Contention: Conversely, guessing a high fixed worker count (e.g., spawning 8 workers on a 4-core CPU) forces the OS scheduler into heavy context-switching, which slows down execution and drains mobile battery life.

Impulse bypasses declarative browser caps to detect the true physical concurrency available, enabling optimal 1:1 worker-to-core allocation.

Return Value

  • Type: number (positive integer, e.g., 2, 4, 8, 12)
  • Guarantee: Always returns at least 1.

Primary Use Case: Dynamic WebAssembly & Worker Pool Sizing

By passing numThreads directly into your Web Worker pool or Wasm runtime initialization, you unlock up to 4x faster parallel throughput on throttled browsers like Safari while eliminating thread contention on lower-core devices.

Code Example

window.sd.numThreads.on((numThreads) => {
  Object.defineProperty(navigator, 'hardwareConcurrency', {
    get: function() {
      return numThreads;
    },
    configurable: true
  });
});

Dynamic Updates: Current Capacity

Because Impulse continuously profiles the device in the background, calling .get() always reflects current execution conditions rather than a stale page-load snapshot.

Note that using .on(handler), handler is called each time the metric changes.