Worker Threads
ELI5 — The Vibe Check
Worker Threads let Node.js actually use more than one CPU core. Normally Node is single-threaded — it can only do one heavy calculation at a time. Worker Threads are like hiring extra brains to do the math homework while the main thread keeps answering HTTP requests.
Real Talk
Worker Threads is a Node.js module that enables true parallel execution of JavaScript code in separate threads sharing the same process memory. Unlike child processes, workers can share memory via SharedArrayBuffer and communicate efficiently through message passing. Ideal for CPU-intensive tasks that would otherwise block the event loop.
Show Me The Code
const { Worker } = require('worker_threads');
const worker = new Worker('./heavy-calc.js', { workerData: { input: data } });
worker.on('message', (result) => console.log('Done:', result));
worker.on('error', (err) => console.error(err));
When You'll Hear This
"We offload image processing to Worker Threads so the API stays responsive." / "Worker Threads share memory, making them faster than child processes for CPU work."
Related Terms
Child Process
A child process is when your Node.js app spawns a completely separate process to do work.
Concurrency
Concurrency is juggling multiple tasks at once — not necessarily at the exact same instant, but switching between them fast enough that they all seem to be...
Event Loop
JavaScript can only do one thing at a time (single-threaded), but the Event Loop is the trick that makes it seem like it can multitask.
Parallelism
Parallelism is doing multiple things at literally the exact same time — two chefs cooking two dishes simultaneously.