To understand NodeJs & its threads, first, some information is necessary on Javascript and NodeJs. NodeJs is a serverside language made upon javascript which was initially designed as a client-side scripting language. Confused by that statement? Well, most beginner-level developers are confused to be honest when they start out with NodeJs. To grasp the essentials of node js one has to first grasp all the essentials of javascript. Then from there move on to what different libraries and frameworks javascript has to offer. And which ones are the most famous and most widely used ones?
it is probably wise to aim for NodeJs after having this initial information. And having truly a good concept of the javascript realm overall. Since NodeJs is designed on top of javascript, it offers everything that javascript does. The variables have the same properties, types, and methods. You can add prototype methods to objects just like plain old javascript, all the event handling, and functional execution is just as same as javascript. But what makes NodeJS truly backend is its ability to stand alone as a server.
Yep, that’s right a real server on apache just as you would have for any other backend language. NodeJS is just as powerful. In some cases for modern development node js is actually preferred over legacy server-side languages.
Understanding javascript
To go into detail and understand javascript truly takes years of experience. As only experienced professionals who have worked for a long time on; vanilla js, also various javascript libraries and frameworks will know more about the inner workings of javascript.
To keep things simple let’s assume a simple web project in which there is a front-end layout. And a user interacts with components on a web page and in return expects some sort of visual confirmation of their actions. This is where javascript kicks in. Javascript acts like a layer of communication and logical decision mechanism between user actions and the system’s responses. Every time an action is made on a webpage it goes into the javascript event loop.
Understanding event loop (broader picture)
The event loop of javascript is managed by the web browser’s engine. All javascript-based actions are stored here temporarily for execution and these actions/operations are queued based upon a priority level set up by the browser engine. The way these tasks are handled based on priorities is called the event loop.
To understand the event loop first you need to understand that javascript is single-threaded and all its frameworks and libraries follow the single-threaded nature under the hood. Hence to optimize the processing and execution time of tasks there was needed a way to categorize tasks based on their nature. And priorities are then defined depending on the nature of the task. For example; if the execution of a task is required to wait and fetch something from API, then it would make no sense to slow down other tasks which do not depend on this API call and these should execute right away.
NodeJs & its threads – Event loop priority
This is exactly what is happening underneath. Here is a brief breakdown of task-specific priorities in the event loop:
- Pure code (javascript functions, statements)
- Promises
- Timer functions
This is the priority in which execution happens in javascript. Firstly, there is the execution of plain javascript code. Then comes the promises and lastly, the third priority is timer functions. The execution time of each of these is different and it totally depends on the amount of execution that is required for the task at hand.
Is nodeJs single-threaded or multi-threaded?
A common misconception among beginner javascript developers is that NodeJS is single-threaded.
It is not, NodeJs has 2 threads. One is for the event loop and the second for the execution.
Although NodeJs is based on javascript and heavily relies on all of its concepts. NodeJs was specifically designed with the 2 thread approach so that it can provide quick responses. Having a separate execution thread helps in focusing all the power of that single thread on the execution of the tasks. And the other thread can this way focus on prioritizing the event loop. Hence, a new level of optimization is achieved this way.