Performance: Web workers for heavy computation
HIGH IMPACT
This concept affects the page's input responsiveness and smoothness by offloading heavy tasks from the main thread.
const worker = new Worker(new URL('./heavy.worker.ts', import.meta.url)); worker.postMessage('start'); worker.onmessage = ({ data }) => { console.log('Result:', data); };
function heavyCalculation() { let result = 0; for (let i = 0; i < 1e9; i++) { result += i; } return result; } heavyCalculation();
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy computation on main thread | N/A | Blocks reflows during computation | Delays paint and composite | [X] Bad |
| Heavy computation in web worker | N/A | No blocking of reflows | Paint and composite proceed smoothly | [OK] Good |