Is Node is Single Threaded?
An experiment to understand Nodejs Single or Multi-Thread Concept.
When we say that node is single thread then what we actually mean is that Node is created with the mindset of an async experiment for async processing by its creator. Means node is just doing async processing on a single thread and could provide more performance and scalability under typical web loads than the typical thread-based implementation.
I am not going to type a lot of text and explanation over here what I will do is I am gonna give you guys a code example so that you can understand if NodeJs is a single thread
To understand what’s going on in here you must have a small idea about how NodeJs works internally and for that, I will suggest you guys go through this article which I have written previously: How NodeJS Works Internally?
If you have gone through the article you must have an idea about how Nodejs works internally now I would like to give you a code example with Nodejs Standard library and run it and then we will observe result for node Single-threaded
In the above code, I have taken a hashing function in which I have passed a few arguments (that is not important to know). At the end of the function there is a callback function which will only be called when crypto.pbkdf2() will completely execute itself. Here I have consoled a time which means that when this function finishes its task just print the time log. I have also taken a start time before starting the function which gives me a complete idea about when it started and when it ended. With this, we can know that how much time does this function take to finishes its task.
Go to the terminal and run node app.js
As I run this code I found 61 milliseconds in my console and above is the screenshot
so we can say this that this whole function takes around 61 milliseconds to complete its task.
Now to test is single thread we gonna replicate this function quite a few times like 2–3 times. One this I need you guys to know very clearly that all these 3 functions that I have called will be called simultaneously means they are independent functions and hence one is not dependent on others to finish their job and that’s where Nodejs Concurrency comes to play in.
Now that they are independent of each other whole task should be done within 1 second as we saw in the above explanation.
Now go to terminal and type node app.js and on my system here is the output
so you can see this that there is all these times are very close to each other means all of these functions are completed their tasks in most probably very relatable time to each other. Which is not possible because if the Node is a single-threaded then the first function should be executed first and then the second one and then the last one because a thread cant execute more then one instruction at a time. Like in the following diagram
But Node doesn’t follow the above pattern and hence we can say that behind the scene Node is Multithread.
In reality, It looks something like this
Now its time to test Nodejs Single-Threaded nature i.e we gonna duplicate that same function like 8 times and run our app.js file with Nodejs in terminal like this:
And on my system here are the result
I have run this file for 5–6 times and results are almost same
If you look closely in the output then you will notice that the last few times are very different from those are in starting. So this gives an idea about this that the first 4 tasks are executed in within like 150 milliseconds but later other every single task took around 280 milliseconds each. Well, why so.
To understand it properly I would like you guys to educate little bit on the thread (You guys must have Idea bout it but just sharing what req. to go through this article )
Programm is collection of instruction that performs a specific task and this instruction and now these instruction can be assigned to thread to complete and Lets suppose that in my computer 2 core processor are available and different core means if we say that 1 core had the ability to execute 100 instruction at a time and your system may have 2 core then this means that when first is getting full then this instruction will be assigned to second core and if that core is also full then operating system will wait till the thread gets executed and next task is scheduled to the any of the processor core. This management of thread scheduling is managed by OS so you don't have to worry about it.
So what's happening behind the scene, Well When I created 8 functions in the app.js file and executed it then processor assigned each of the task to all available with thread at a particular time and when it noticed that there are more instructions to perform then what is actually available to processor then it waited for the task to get executed and when processor gets free from previously given instruction then it reassigned that part of the processor with next instruction which needed to be executed in app.js.
You don't have to worry about this juggling of a task in processors as it is totally managed by the operating system and all the algorithm regarding this is written its core.
So you can see Nodejs is not actually a single thread event loop. It actually depends on several other parts of the processor for its execution.
Not only this all the network calls that we make in our code is actually not even managed by Node or its core library libuv(which is written in C++ and handles all our I/O and Network calls) but it actually depends on hardware for its I/O call, for example, reading any file from your local storage requires a lot of instruction and file check in it and that is managed by Operating system and until and unless these checks don’t go right or correctly Node is not allowed to read file. Also, all the networking calls are managed by the threads which are totally taken care by operating system and Node literally had not to worry about any of its functionality.
Let's take an example to prove the above statement
I have created above javascript file and made a network call in it with the help of standard module http provided by Node itself. And below that, there is this hash calculating function. We will call this several times and try to execute all these functions at the same time.
When I executed this file following are the result
If you look into the output you will come to know that Network call (Task 1) take as the maximum amount of time to execute itself and other functions are very close to each other. Well if your internet is fast then it might be possible that your network call takes like near around 60 millisecond and other tasks are close to each other.
Why did this happen?
Well, when I said that network calls are only managed by the operating system and libuv only act as a mediator in between node and operating talking to each other in terms of request and response. So the operating system takes care of this task by itself and assigned to a different thread and this is the reason that its execution time is a lot different than another task that is executing with it.
So we can see this that Node is not actually single thread and works on multithreading behind the scenes.
Hope you like this and my sharing would have helped you in some way. Put the clap to encourage me to put more articles like this.