Skip to content
tezvyn:

Which Web API offloads expensive work from the main UI thread?

Source: developer.mozilla.orgEasyHow cards are made

Which Web API offloads expensive work from the main UI thread?
Summary

knowledge of moving CPU-heavy work off the main thread.

Key points

cite Web Workers, instantiate new Worker(url), and communicate via postMessage and onmessage.

Watch out for

citing setTimeout or async/await, which still run on the main thread.

What's really being asked

This question checks if you know that standard JavaScript runs on a single main thread and that heavy computation blocks UI updates. The interviewer wants to see that you can name a true threading primitive available in browsers and explain how to spin it up.

The full answer

First, name the Web Workers API as the correct tool for running JavaScript in a background thread. Second, describe initiation: you create a dedicated worker by calling new Worker and passing the URL of a script file, for example new Worker('processor.js'). Third, explain the message-passing contract: the main thread sends data to the worker using worker.postMessage(largeJsonObject), and the worker processes it and sends results back via self.postMessage(result). Fourth, note that the main thread listens for replies by assigning a function to worker.onmessage, reading event.data to update the UI. Fifth, mention that workers cannot access the DOM directly, so all UI updates must happen on the main thread after receiving the processed data.

The mistakes people make

A red flag is suggesting setTimeout, requestIdleCallback, or async await as a fix. These still schedule work on the main thread and will freeze the UI if the task is CPU-bound. Another mistake is proposing to move the JSON parsing into a Promise without a worker; Promises do not create new threads. Some candidates also forget that the Worker constructor requires a separate script file or a valid URL, and they omit the postMessage step entirely.

What usually comes next

The interviewer may ask how to transfer large buffers efficiently without copying, which is when you would mention the Transferable Objects syntax in postMessage. They might ask about SharedArrayBuffer for true shared memory, or how to handle errors in a worker using the onerror event. Another follow-up is how to terminate a worker with worker.terminate() when the job is done to free resources.

A concrete example

Imagine a dashboard that receives a ten megabyte JSON payload. On the main thread, calling JSON.parse would block interactions for several hundred milliseconds. Instead, you instantiate a worker with const worker = new Worker('json-parser.js'). In the main file, you fetch the file and then call worker.postMessage(jsonText). Inside json-parser.js, you write self.onmessage = function(event) { const data = JSON.parse(event.data); self.postMessage(data); }. Back in the main thread, worker.onmessage receives the parsed object in event.data and renders the charts without dropping frames.

Interview question

You need to process a large JSON payload without blocking UI updates. Which approach actually uses a separate browser thread?

  • a.Wrapping the logic in an async function with await
  • b.Using requestIdleCallback to run during idle periods
  • c.Instantiating a Worker and passing data via postMessageCorrect
  • d.Deferring the task with setTimeout
Why?

Web Workers run JavaScript in a dedicated background thread and communicate with the main thread via postMessage, preventing UI blocking. setTimeout, async/await, and requestIdleCallback all schedule work on the main thread, so CPU-intensive tasks will still freeze the interface.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles