Performance: Web Workers for parallel processing
Improves interaction responsiveness by offloading heavy tasks from the main thread to background threads.
const worker = new Worker(new URL('./worker.ts', import.meta.url).href, { type: 'module' }); worker.postMessage(data); worker.onmessage = (e) => console.log(e.data);
const result = heavyComputation(); // runs on main thread console.log(result);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy computation on main thread | Minimal | Blocks reflows during execution | Blocks paint updates | [X] Bad |
| Heavy computation in Web Worker | Minimal | No reflows blocked | No paint blocking | [OK] Good |
postMessage.worker.postMessage('Hello') is valid and recognized.const worker = new Worker(new URL('./worker.ts', import.meta.url).href, { type: 'module' });
worker.onmessage = (e) => console.log('Result:', e.data);
worker.postMessage(5);self.onmessage = (e) => {
const num = e.data;
self.postMessage(num * 2);
};num.const worker = new Worker('./worker.ts', { type: 'module' });
worker.postMessage('start');
worker.onmessage = (e) => console.log(e.data);
worker.terminate();Pass the test and this pattern moves unknown