Featured image of post Node.js Worker Threads in a Nutshell

Node.js Worker Threads in a Nutshell

Exploring Worker Threads in Node.js 10

Node.js 10 - Worker Threads in a Nutshell

So, you’ve heard about Worker Threads in Node.js 10 and wondered, *“Wait, isn’t Node.js single-threaded?

What is this sorcery?”* Well, my friend, let’s crack open this multithreaded nutshell and see what’s inside.


Why Worker Threads?

Node.js is fantastic for I/O-heavy applications, thanks to its event-driven, non-blocking architecture.

But when it comes to CPU-intensive tasks, like processing large files, number crunching, or running AI models, things get a little… sticky.

The single-threaded event loop gets blocked, and your server starts acting like a sluggish sloth.

That’s where Worker Threads come in!

🏋️


What Are Worker Threads?

Before Node.js 10, if you wanted to leverage multiple cores, you had to spawn child processes or use clustering.

But these approaches had some downsides:

  • Child processes don’t share memory, so they need inter-process communication (IPC), which adds overhead.

  • Clusters duplicate the entire process, consuming more resources.

Worker Threads, introduced in Node.js 10, allow true multithreading inside a single process.

They share memory through SharedArrayBuffer and can communicate efficiently via message passing.

🎉


How to Use Worker Threads

Using Worker Threads is actually super easy.

Let’s start with a basic example.

Step 1: Import the worker_threads Module

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

Step 2: Create a Worker

If you’re in the main thread, you can create a worker like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
if (isMainThread) {
    const worker = new Worker(__filename);
    worker.on('message', msg => console.log(`Worker says: ${msg}`));
    worker.postMessage('Hello from main thread!');
} else {
    parentPort.on('message', msg => {
        console.log(`Main thread says: ${msg}`);
        parentPort.postMessage('Hello from worker thread!');
    });
}

Step 3: Run It!

When you run this file with node script.js, you’ll see the two threads chatting with each other.

Congrats, you just wrote a multithreaded Node.js app!

🎉


When to Use Worker Threads

You don’t always need Worker Threads.

Here’s when they make sense:

✅ CPU-intensive tasks (e.g., large data processing, cryptography, image processing)
✅ Background tasks that shouldn’t block the event loop
✅ Offloading work from the main thread while keeping everything in a single process

And here’s when you don’t need them:

❌ Simple I/O-bound tasks (e.g., database queries, file reading) – just use async/await
❌ When running too many workers can consume too much memory


Basic Ideas

  • Worker Threads allow true multithreading in Node.js 10 and later.

  • They are great for CPU-intensive tasks but unnecessary for I/O-bound tasks.

  • Unlike child processes, they can share memory, reducing overhead.

  • Use worker_threads module to create workers and communicate via messages.

  • If your app is slowing down due to CPU-bound work, Worker Threads can save the day! 🦸


Key Ideas

ConceptExplanation
Worker ThreadsA new feature in Node.js 10 that enables multithreading.
Shared MemoryWorkers can share memory via SharedArrayBuffer.
Message PassingWorkers communicate with the main thread using messages.
CPU-Intensive TasksBest use case for Worker Threads to offload heavy computations.
I/O-Bound WorkShould still be handled with async/non-blocking methods.

References