I want to welcome you all with one more knowledge gain topic. We know how fast NodeJS get attention towards developers. Now, the Developer suggests NodeJs because of its power. So, Do you know what's the power of NodeJs? I think you should know if you don't know because NodeJS is now the most popular choice for backend development. And I have seen many of the microservice-based projects developed in Python, Java earlier now in the path to use NodeJs for new microservice development. So, Why the NodeJS getting popular day by day. Let's understand it.
When I joined my first company after my college then my first project was in NodeJs. And previously I was familiar with synchronous languages. So, I was getting into trouble because of the asynchronous nature but over the period I understand NodeJS and its non-blocking asynchronous nature. And my perspective toward the NodeJs changed. And I want to tell you the power of NodeJS which make me fall in love ❤️.
Let's understand the basics of NodeJS
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine
If you will going to open the NodeJS website then you will see the above definition. As we know NodeJs uses Javascript but the Javascript earlier only ran on the browsers for creating dynamic pages. So, now the point is how we are using NodeJS as backend language. So, the answer is the above definition. Basically, the NodeJs developer created one engine same as the java runtime environment (JRE) which uses Chrome's V8 engine in the background. V8 Engine basically compiles the Javascript code and creates an environment to execute it. Chrome uses V8 Engine, Firefox uses SpiderMonkey. Engines help the browser to understand Javascript.
And NodeJS is non-blocking asynchronous in nature and that nature is provided by Event Loop.
Event Loop
Without understanding the Event Loop we can not understand the power of NodeJS. As we know Javascript is single-threaded but still, NodeJS can serve more req/sec than most of the frameworks available out there. And that is achieved by non-blocking asynchronous nature. In simple words, NodeJS doesn't block the thread when NodeJS perform an asynchronous task. This means we can perform multiple tasks concurrently. But how as we know NodeJS is single-threaded? So, the answer is for running asynchronous tasks NodeJS uses the Libuv library internally which is implemented in C++ and that library gives NodeJS power to perform asynchronous tasks. So, this way NodeJS run multiple asynchronous tasks in the background. And when the task gets complete kernel tells NodeJS so, the appropriate callback may be added to the poll queue to eventually be executed.
Event Loop has so many phases like
┌───────────────────────────┐
┌─>│ timers │
│ └─────────────┬─────────────┘
│ ┌─────────────┴─────────────┐
│ │ pending callbacks │
│ └─────────────┬─────────────┘
│ ┌─────────────┴─────────────┐
│ │ idle, prepare │
│ └─────────────┬─────────────┘ ┌───────────────┐
│ ┌─────────────┴─────────────┐ │ incoming: │
│ │ poll │<─────┤ connections, │
│ └─────────────┬─────────────┘ │ data, etc. │
│ ┌─────────────┴─────────────┐ └───────────────┘
│ │ check │
│ └─────────────┬─────────────┘
│ ┌─────────────┴─────────────┐
└──┤ close callbacks │
└───────────────────────────┘
You can read about each phase in detail with this link.
Event Loop is basically a program that basically runs continuously and interprets our javascript program and decides which code needs to be in which phase. Let's take an example code and understand the event loop.
console.log("Hi!");
setTimeout(function timeout() {
console.log("I am asynchronous!");
}, 5000);
console.log("Welcome to my blog.");
So, when the above code executes then it runs line by line because javascript uses an interpreter.
Javascript run this line of code from starting and push it into the stack
console.log("Hi!");
console.log is a synchronous task that's why it executes by a V8 engine. and printed as Hi!. And then that line of code pop out from the stack.
Then javascript run next line
setTimeout(function timeout() { console.log("I am asynchronous!"); }, 5000);
when this line is interpreted by the javascript compiler then it is first pushed to the stack. But the setTimeout is an asynchronous task that's why it can not be executed immediately and javascript doesn't want to block the thread too. So, all asynchronous jobs pop out from the stack and are handled by web APIs or asynchronous libraries like libuv. In the meanwhile, the javascript compiler compiles the next piece of code and pushes it into the stack.
console.log("Welcome to my blog.");
And the asynchronous task handlers handle the asynchronous task till the setTimeout waits for a defined minimum time duration. After that timeout() function is pushed to the event queue where it waits for dequeue till the stack is not empty. Once the stack is empty task will be dequeued and pushed back to the stack for execution.
And the Output for the Above code will be
Hi!
Welcome to my blog.
I am asynchronous!
You can check the whole code execution in the event loop on this link.
So, It's just about the NodeJS asynchronous nature. But do you know NodeJS can use multiple threads or we can create the new child process? Yes, we can run multiple child processes and threads in NodeJS. By using NodeJS core module child_process and worker_threads. And there is one more core module cluster which is used for scaling the NodeJS server by creating multiple worker threads. You can learn this topic in detail on NodeJS website.
Now, One more special feature of NodeJS enables the developer to use C and C++ native libraries by using N-API. Which increases the performance of code because of the native library build in C, C++.
And now the most popular feature provided by NodeJS Streaming APIs which helps NodeJS to use less memory with higher throughput. And NodeJS is basically the best fit for the IO operations because of its Asynchronous non-blocking nature.
NodeJS core module gives so many other features which we can make use of it and we also know npm gives numerous third party packages which make our work very easy to perform.