Worker Threads in Node JS

Saurabh Kumar
KhojChakra
Published in
4 min readSep 26, 2020

is Node JS became multi-threaded now..??

it’s a first question lot of people, who are new to Node JS, ask me when they came to know about worker threads in node and answer is NO..!!

let's try to understand, why the node js is still single-threaded even though it has worker thread. even before that we need to understand what the worker thread is..??

worker thread module in node js is based on Web Workers that are available in the browsers and are a simple means for web content to run scripts in background threads.

As per the Node documentation, The worker_threads module enables the use of threads that execute JavaScript in parallel. To access it:

const worker = require('worker_threads');

Workers (threads) are useful for performing CPU-intensive JavaScript operations. They will not help much with I/O-intensive work. Node.js’s built-in asynchronous I/O operations are more efficient than Workers can be.

Unlike child_process or cluster, worker_threads can share memory. They do so by transferring ArrayBuffer instances or sharing SharedArrayBuffer instances.

worker thread provides you APIs to deal with CPU intensive tasks while still maintaining the responsive application

it gives CPU intensive workload to another thread while keeping your main thread available for new user request

each worker thread is the new instance of the event loop, creating a worker thread gives you a new event loop, so the event loop running in the main thread is free to take new request if you run CPU intensive task in a worker thread. one good thing is if any worker thread running out of memory then it will not take down other worker thread

worker threads don’t have all the mechanism for thread synchronization like in other multi-threaded programming languages, however, we can pass messages back and forth between a worker thread and the main thread.

Now let's see how a worker thread can take the load off the main thread and keep your app responsive. Below is the minimal example of worker thread in action

here from the above code, you can see that we can instantiate a new worker from Worker constructor. isMainThread check if it is the main thread. The worker thread will begin it’s execution once instantiate. parentPort is a communication port back to the main thread. It is also an event emitter. Whatever you passed, the Post message will be passed as a message event back to the main thread. The name of the event sent across the parent port is message . we have added an event handler in the main thread, which will for now just log the message received from a worker thread.

okay..!! let’s now run the above code and see how it works. below I have attached the screenshot from my machine, you can compare it with your output.

here you can observe that the main event loop was not blocked because of the worker thread doing some CPU intensive task. we immediately get the output from the main thread. the message is passed back from the worker got handled and logged later, the cpuIntensiveTask function is still blocking an event loop. It is blocking the Worker’s event loop, which left the main event loop free, to continue processing the user’s request.

here we saw how we can pass messages from the worker thread to the main thread. now let’s see how we can pass data along with the messages from the main thread to the worker thread.

for that, we have to importworkerData from worker_thread then use it to pass data to the worker by passing an object to the worker constructor.

below is the output you will get when you run the above code.

I hope i was able to give some idea about worker_threads always remember that worker_thread is only meant to be used for CPU intensive task..!!

please clap 👏 if you found this article useful. cheers 🍺

--

--

Saurabh Kumar
KhojChakra

a guy who believe in spreading love, peace and knowledge.. a Senior Software Engineer working remotely