Concept
Worker threads
Running CPU-heavy JavaScript on separate threads so the event loop stays free.
Worker threads explained
Worker threads let a Node.js program run JavaScript in parallel on other CPU cores. Node normally runs your code on one thread with an event loop, which is ideal for I/O but blocks every request while a CPU-heavy task runs. A worker runs in its own thread with its own event loop, so heavy work such as image processing, compression or large calculations no longer freezes the server.
How worker threads work
// main.js
import { Worker } from "node:worker_threads";
function hashInWorker(data) {
return new Promise((resolve, reject) => {
const worker = new Worker(new URL("./hash-worker.js", import.meta.url), { workerData: data });
worker.once("message", resolve);
worker.once("error", reject);
});
}
// hash-worker.js
import { parentPort, workerData } from "node:worker_threads";
parentPort.postMessage(expensiveHash(workerData));
Workers do not share ordinary variables with the main thread. They communicate by passing messages, which copies the data, or by transferring an ArrayBuffer, or by sharing memory through SharedArrayBuffer with Atomics for coordination.
Workers, cluster or child processes
- Worker threads: CPU-heavy tasks inside one process; cheaper than processes, can share memory.
- Cluster: several copies of a server process sharing one port, to use all cores for handling requests.
- Child processes: running other programs, or isolating risky work completely.
Common mistakes
- Using workers for I/O. Network and file operations are already non-blocking; workers add overhead.
- Starting a new worker per request. Startup is costly; use a pool of reusable workers.
- Sending huge objects back and forth, where copying costs more than the work saved.
What interviewers ask about worker threads
- "Is Node.js single-threaded?" Your JavaScript runs on one main thread; libuv uses a thread pool for some I/O, and worker threads add more JavaScript threads.
- "When would you use a worker?" When a synchronous CPU task blocks the event loop.
- "How do threads communicate?" Messages, transferred buffers or shared memory.
Measuring before you reach for workers
Confirm the event loop is really blocked first: measure event loop delay with perf_hooks.monitorEventLoopDelay() or a profiler. If a single request holds the loop for tens of milliseconds while it computes, a worker pool is the right fix; if time is spent waiting on the network or database, it is not.
What should you learn before Worker threads?
- FunctionsReusable blocks of code; in JavaScript they are values that can be passed around and returned.
- CallbacksFunctions passed to other functions to be called later, the original way to handle async work.
- The event loopWorker threads exist to keep the event loop free.
Connections to other concepts
- Part of
- Node.js
Practise Worker threads
- Top 20 JavaScript interview questions with answers: core JS, Node.js and React1 question on Worker threads
- Advanced Node.js Interview Questions1 question on Worker threads