Lesson 3 of 30
JavaScript is single-threaded — one heavy computation freezes the whole page. A Web Worker runs real JavaScript on a separate thread so it doesn't have to.
12 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
A Web Worker mutates the hugeDataset object it received via postMessage. Does this affect the original hugeDataset on the main thread?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
A strong answer covers
Likely follow-ups
From the event loop lesson: JavaScript runs on one thread. A long synchronous task — sorting 500,000 rows, parsing a huge JSON blob, running an image filter — blocks that thread completely. No clicks register, no scrolling happens, nothing paints, until the task finishes.
A Web Worker runs JavaScript on a genuinely separate thread. Move the heavy computation there, and the main thread stays free to keep the page responsive while the worker crunches numbers in the background.
// main.js — freezes the page for however long this takes
const filtered = hugeDataset.filter((row) => row.value > threshold).sort(compareFn);
render(filtered);
// main.js — with a worker, the main thread stays responsive
const worker = new Worker("filter-worker.js");
worker.postMessage({ hugeDataset, threshold });
worker.onmessage = (e) => render(e.data.filtered);
// filter-worker.js — runs on its own thread
self.onmessage = (e) => {
const { hugeDataset, threshold } = e.data;
const filtered = hugeDataset.filter((row) => row.value > threshold).sort(compareFn);
self.postMessage({ filtered });
};
The worker can't touch the DOM at all — no document, no window. It only computes, and hands the result back via postMessage.
A heavy computation on the main thread blocks everything — the UI freezes until it finishes.
SharedArrayBuffer is the advanced exception, used rarely and carefully — most real usage relies on copying.)At small scale, main-thread jank is invisible. At the scale of a spreadsheet app, a data dashboard, an image or video editor, or a code editor doing syntax highlighting on a huge file, that same jank becomes a measurable, user-visible stutter — and at millions of users, a 200ms freeze during scroll is a real, frequent complaint, not a rare edge case. Companies operating at that scale reach for workers specifically to keep expensive computation from ever blocking the frame that would otherwise drop.