Skip to main content

Command Palette

Search for a command to run...

the Power of Worker Threads in Node.js

Updated
2 min read

A while back, Node introduced the worker_threads module. It allows parallel execution, Real multi-tasking.

How you actually code it

It’s surprisingly simple. You just need two files.

The Bottleneck

Lets understand this with a very simple and realistic example.

  • Take a folder of high-res nature photos.

  • Resize them, blur them, and grayscale them.

  • Set a timer on it

I already did it on my machine and here are the results. Running this on the main thread took over 11 seconds for just 10 images. For those 11 seconds, the app was dead. WHEN I tried to ping the homepage, it timed out. In the real world, that’s unacceptable.

Now, lets see the worker way.

1. The Manager

// index.js
const { Worker } = require('worker_threads');

function processImageInBackground(filePath) {
  return new Promise((resolve, reject) => {

    const worker = new Worker('./image-worker.js', {
      workerData: { filePath } 
    });

    worker.on('message', (result) => {
      console.log(`Worker said: ${result}`);
      resolve(result);
    });

    worker.on('error', reject);
  });
}

processImageInBackground('./huge-photo.jpg');

2. The Worker

// image-worker.js
const { parentPort, workerData } = require('worker_threads');
const jimp = require('jimp'); 


const { filePath } = workerData;


async function run() {
  const image = await jimp.read(filePath);
  await image.resize(100, 100).writeAsync(`processed/output.jpg`);

  parentPort.postMessage('Done processing!');
}

run();

The "Whoa" Moment 🤯

I took that same 11-second task and refactored it to use Worker Threads. spun up a worker for each image so they all processed at the exact same time.

The result? just took 4 seconds.

That is a nearly 3x speed improvement. But more importantly, the main thread wasn't blocked. The server stayed responsive the entire time.

TL;DR

If you’re doing heavy CPU work in Node.js, stop blocking the event loop.

  1. Use worker_threads.

  2. Offload the heavy math/image processing to a separate file.

  3. Use a Pool to manage them so you don't fry your server.