Web Workers: Keep Your UI Responsive During Heavy Tasks

A Web Worker is like a background helper, running heavy JavaScript tasks in a separate thread so your UI never freezes. Use it for complex calculations or data fetching. The main footgun: workers cannot directly manipulate the DOM.
Why it exists
JavaScript is single-threaded. If a long-running script executes, the browser can't do anything else—it can't respond to clicks, update animations, or even scroll. The UI freezes. Web Workers were created to solve this by enabling true multi-threading in the browser, allowing long tasks to run without blocking the main thread.
The mental model
Think of the main browser thread as a busy front-desk receptionist who also handles all user interactions. If they get stuck on a long, complex phone call (a heavy script), no one else can be served. A Web Worker is like hiring a back-office employee. The receptionist can pass off a complex task to the back-office worker and continue serving users. The back-office worker will send a message back when the task is complete.
How it works
You instantiate a worker by providing the path to a JavaScript file: const myWorker = new Worker('worker.js');. This script runs in a separate global context, isolated from the main thread's window object. Communication is handled through an asynchronous messaging system. The main thread uses myWorker.postMessage(data) to send data, and the worker script uses self.postMessage(data) to send it back. Both listen for incoming messages with an onmessage event handler. Importantly, data is copied, not shared, when passed between threads.
When to use it
Use Web Workers for CPU-intensive operations that would otherwise block the UI. This includes things like complex mathematical calculations (e.g., image filtering, cryptography), parsing large JSON files, or managing data in a complex client-side database without freezing the page. They are also useful for pre-fetching data or performing background synchronization.
When not to use it
Don't use a worker for tasks that require direct, synchronous access to the DOM or the window object, as workers are explicitly forbidden from this. The overhead of creating a worker and passing messages also makes them unsuitable for very small, quick tasks. For simple asynchronous operations that don't block the CPU, like a standard fetch call, a Promise or async/await on the main thread is often sufficient.
One canonical example
A common use case is processing a user-uploaded image. The main thread receives the file and sends the image data to a worker using postMessage(). The worker then performs a heavy operation, like applying a grayscale filter pixel by pixel, without freezing the UI. Once done, the worker sends the modified image data back to the main thread, which then displays the result in a canvas element.
Interview question
What is the primary reason to use a Web Worker in a web application?
- a.To perform CPU-intensive tasks without blocking the main thread and freezing the user interface.Correct
- b.To enable direct manipulation of the Document Object Model (DOM) from a background thread.
- c.To replace all asynchronous operations like network requests with a more efficient background process.
- d.To share JavaScript variables and objects directly between the main thread and background tasks.
Why? this is the answer
Web Workers are designed to offload heavy, CPU-intensive tasks to a separate thread, preventing the main UI thread from freezing. They cannot directly manipulate the DOM, and data is copied, not shared, between threads. While they handle background tasks, they are not intended to replace all simple asynchronous operations like network requests due to overhead.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #web workers
- #javascript
- #performance
- #web apis
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.
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