Open In App

Is Node.js entirely based on a single-thread ?

Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. In this article, we’ll try to dig a little deeper and understand if Node.js is entirely based on a single thread. However, before starting out, let’s clear a few of our basics:

what actually is meant by a framework being single thread based?



When we say that a framework is single-threaded, that means only one statement is executed at a single time. Whatever is on top of the call stack, gets called in a non-blocking fashion.

Below is the Node.js Server Architecture:



Node.js Architecture

Node.js uses the Single Threaded Event Loop design to manage the numerous concurrent clients. The JavaScript event-based architecture and JavaScript callback mechanism serve as the foundation for the Node.js processing model. A single thread is used to execute a Node.js application, and this thread also houses the event loop. The event loop is at the core of the Node.js processing model. Let us try to understand how the event loop function.

Event Loop: The event loop only performs a single process at a time. This means that it can only execute one function at a time, and since functions can include multiple instructions, the event loop will only execute one instruction at a time. The beauty of the event loop is that it has the ability to “put aside” time-consuming I/O activities so that other instructions can continue to execute without interruption. This is different from running everything on a single thread. This is why, despite the possibility of numerous users simultaneously sending queries to a Node.js API, we receive quick results. Therefore, we may say that Node.js is single-threaded.

How Node.js works as Asynchronous?

However, through the use of worker threads, we can execute instructions as an Asynchronous model. Internally, Node.js makes use of the libuv library, which is a C++ library that manages operating system-related tasks like concurrency, networking, and asynchronous I/O-based operating systems. 

This library creates a thread pool of four threads to execute OS-related tasks while making use of all the CPU cores’ processing capacity. It also helps in managing all the other threads. Hence, libuv provides Node.js with multi-threaded capabilities in some cases.

To demonstrate this, let us look at an example:

Example 1: In this example, I have used the pbkdf2 function from the crypto library. I have logged the time it takes from the beginning of the program to run a particular function call.




import crypto from 'crypto';
  
const startTime = Date.now();
  
function logTime() {
    crypto.pbkdf2("a", "b", 100000, 512, "sha512", () => {
        console.log("Hash: ", Date.now() - startTime);
    });
}
  
logTime();
logTime();
logTime();
logTime();

Output:

 

Explanation: As you can see all four function calls, and finish almost at the same time. This is not possible in a single-threaded system. Hence, multiple threads are being used above.

Example 2: In this example, we’ll now for a change, let’s call the function logTime five times:




import crypto from 'crypto';
  
const startTime = Date.now();
  
function logTime() {
    crypto.pbkdf2("a", "b", 100000, 512, "sha512", () => {
        console.log("Hash: ", Date.now() - startTime);
    });
}
  
logTime();
logTime();
logTime();
logTime();
logTime();

Output:

 

Explanation: As you can see four of the function calls execute almost at the same time while the last function call takes a huge time. As the computer has four cores, the first four function calls are executed simultaneously, one function call per core while the last function call is run as soon as any of the cores is free.


Article Tags :