0
0
NodejsHow-ToBeginner · 4 min read

How to Use Worker Threads in Node.js for Parallel Tasks

In Node.js, you use worker_threads module to run JavaScript code in parallel threads. Create a Worker by giving it a script or function, then communicate with it using messages. This helps run CPU-heavy tasks without blocking the main thread.
📐

Syntax

The worker_threads module provides the Worker class to create new threads. You import it using const { Worker } = require('worker_threads'). Then, create a worker with new Worker(filenameOrCode, options). Use worker.postMessage() to send data and worker.on('message') to receive data.

Key parts:

  • Worker: Creates a new thread running a script.
  • postMessage: Sends messages to the worker.
  • on('message'): Listens for messages from the worker.
javascript
const { Worker } = require('worker_threads');

const worker = new Worker('./worker.js');

worker.postMessage('start');
worker.on('message', (msg) => {
  console.log('Message from worker:', msg);
});
💻

Example

This example shows how to create a worker thread that calculates the sum of numbers from 1 to 10 million without blocking the main thread. The main thread sends a start message, and the worker replies with the result.

javascript
const { Worker, isMainThread, parentPort } = require('worker_threads');

if (isMainThread) {
  // Main thread
  const worker = new Worker(__filename);
  worker.on('message', (result) => {
    console.log('Sum from worker:', result);
  });
  worker.postMessage('start');
} else {
  // Worker thread
  parentPort.on('message', (msg) => {
    if (msg === 'start') {
      let sum = 0;
      for (let i = 1; i <= 10_000_000; i++) {
        sum += i;
      }
      parentPort.postMessage(sum);
    }
  });
}
Output
Sum from worker: 50000005000000
⚠️

Common Pitfalls

Common mistakes when using worker threads include:

  • Trying to share variables directly between threads (each thread has its own memory).
  • Not listening for error or exit events on workers, which can cause silent failures.
  • Blocking the main thread with heavy computation instead of offloading it.
  • Forgetting to terminate workers when done, causing resource leaks.

Always communicate via messages and handle errors properly.

javascript
const { Worker } = require('worker_threads');

// Wrong: Blocking main thread
function heavyTask() {
  let sum = 0;
  for (let i = 0; i < 1e9; i++) sum += i;
  return sum;
}
console.log('Result:', heavyTask());

// Right: Offload to worker
const worker = new Worker('./heavyTask.js');
worker.on('message', (result) => console.log('Worker result:', result));
worker.on('error', (err) => console.error('Worker error:', err));
worker.on('exit', (code) => {
  if (code !== 0) console.error('Worker stopped with exit code', code);
});
📊

Quick Reference

FeatureDescription
worker_threadsModule to create and manage worker threads
WorkerClass to create a new thread running a script
postMessageSend data to a worker thread
on('message')Receive data from a worker thread
isMainThreadBoolean to check if code runs in main thread
parentPortCommunication channel inside worker thread
terminate()Stop a worker thread

Key Takeaways

Use the worker_threads module to run CPU-heavy tasks in parallel threads.
Communicate between main and worker threads using postMessage and message events.
Each worker has its own memory; share data only via messages or SharedArrayBuffer.
Always handle worker errors and exit events to avoid silent failures.
Terminate workers when done to free resources.