Web Vitals & Performance (50 Qs)•Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)
Web Workers: Offloading Heavy Work from the Main Thread.
senior LevelCode ExampleSystem Design
In Simple Words:
JavaScript is single-threaded; heavy tasks (CSV parsing, image compression, cryptographic hashing, vector search) freeze the main thread, spiking INP. Web Workers run scripts in a separate background thread with zero access to the DOM, communicating with the main thread via message passing (postMessage).
typescript
// worker.ts: Background thread
self.onmessage = (event) => {
const result = parseHeavyDataset(event.data);
self.postMessage(result);
};
// main.ts: Main UI thread remains fluid and responsive (0ms INP delay)
const worker = new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' });
worker.postMessage(hugePayload);
worker.onmessage = (e) => { updateUI(e.data); };