Dedicated Web Workers

A dedicated worker is a background thread owned by its page, for parsing, image processing or any loop that would block input. With { type: 'module' } (Baseline since 2023) the worker can use import; classic workers call importScripts().

fib-worker.js and the page script that starts itJavaScript
// fib-worker.js
const fib = (n) => (n < 2 ? n : fib(n - 1) + fib(n - 2));
self.onmessage = (e) => postMessage(fib(e.data));
// page: <script type="module">
const worker = new Worker('fib-worker.js', { type: 'module' });
worker.onmessage = (e) => { console.log('fib(40) =', e.data); worker.terminate(); };
worker.postMessage(40);
console.log('the page stays responsive meanwhile');
Output
the page stays responsive meanwhile
fib(40) = 102334155

Size a small worker pool by navigator.hardwareConcurrency, and transfer big ArrayBuffers with postMessage(buf, [buf]) instead of copying them. Comlink 12,797 (https://github.com/GoogleChromeLabs/comlink 12,797 ) (Apache-2.0) wraps a worker in a Proxy, turning messages into async calls.