availableThreads
availableThreads 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:
- Browser Spoofing & Caps: Safari and privacy-hardened browsers artificially cap
navigator.hardwareConcurrency(frequently reporting2or4even on 8-core or 12-core chips) to mitigate device fingerprinting. - 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 availableThreads 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
impulse.availableThreads.on((availableThreads) => {
Object.defineProperty(navigator, 'hardwareConcurrency', {
get: function() {
return availableThreads;
},
configurable: true
});
});Dynamic Updates: Current Capacity
Impulse computes the metrics progressively: ScaleDynamics servers can refine a value during the session, for example after more measurements or when the workload of the device changes. .wait() and .once() give the first value known, .value the latest one, and .on(handler) calls handler with each new value. See Progressive Computation.