Did you find this article useful? Share It!

In this article, we will have an introduction of Node JS. We will discuss, why Node JS runtime is appreciated by our industry. What are the internals of Node JS? How does Node JS process our application code internally. So, let’s get started –


What is Node JS?

The official definition of Node JS is – Node JS is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Let’s say, if you are from Microsoft .NET background. As you know that, Microsoft .NET Framework has CLRCommon Language Runtime and if you are from Java background, you know that in Java we have JVMJava Virtual Machine. I believe that similarly, we have JavaScript runtime which allows us to run JavaScript outside browser.

Node JS is a set of bindings to Google’s V8 JavaScript VM [Virtual Machine]. It allows one to script programs that do I/O in JavaScript mainly focused on performance.

What is V8 JavaScript engine?

V8 is a JavaScript Execution Engine built for Google Chrome. It was open sourced by Google in year 2008. It is written in C++. V8 compiles JavaScript source code into Native code instead of interpreting in real time.

Introduction to LIBUV –

Node.js uses libuv to handle asynchronous events. Libuv is a software library – provides asynchronous event notifications. It offers –

  • Asynchronous TCP and UDP sockets
  • Asynchronous file and file system operations
  • File system events
  • Thread Pool, Signal handling, threading and synchronization
  • And many more functionalities.

We work with two types of operations in our day-to-day life. The first one if non-blocking operations and second one is blocking operations. Let’s see where exactly these operations get executed. Non-blocking operations get executed mainly in L1, L2 cache and RAM. Let’s say you are doing some calculations, manipulations. These all operations get processed in L1, L2 cache and in RAM. The number of cycles are very less during these operations process. However, when you talk about blocking operations, these blocking operations are Disk and Network operations. These operations take millions of cycle before we get the result. So, in general, these operations are blocking operations.

Now the big question is how do we handle these blocking operations in development? This is where we use multi-threading/multi-tasking. Other threads can execute while the blocking code runs. But is that the best option we have which working with blocking operations? The reason this question arises is, when you work with multi-threading, the context switching between the threads are not free. Also note that, execution stack takes memory. For massive concurrency, we can not use an operating system thread for each connection.

However, we can also work with Green Threads. In our world, Green threads are the threads that are scheduled by a runtime library or virtual machine (VM) instead of natively by the underlying operating system. Green threads or co-routines can improve the situation dramatically. But there is still the machinery involved to create the illusion of holding execution on I/O.

This is where comes Event Loop. But what is Event Loop? Let’s have an introduction to Event Loop in Google Chrome’s V8 Engine.

First of all you need to know that JavaScript is single thread. Means we have exactly one execution stack. In JavaScript execution engine, we have Event Loop. When you work with JavaScript, we implement synchronous and asynchronous operations. Any function in JavaScript which has callback function [Parameter to a function], is known as asynchronous operation. For example, AJAX calls, DOM Events, Timer functions etc. When start executing JavaScript code, the execution engine will place this code in execution stack. After that, it checks if the function is synchronous or asynchronous. If the function is synchronous, it will get executed in execution stack and the result will be returned. However, if the operation is asynchronous, the execution engine will handover this operation to one of the thread from the worker thread pool and continue to execute the next code bock.

Eventually, the operations which are getting processed by the worker threads, they will get completed. These completed operations will get placed in a special location called Event Queue. When these is nothing in our execution stack, Event Loop will pick up the operations one-by-one and place them in execution stack. This is where the callback function will get executed and the result will be processed. So, this is a small introduction of Google Chrome’s V8 engine.

JavaScript language is specially designed to work with Event Loop as it offers –

  • Anonymous functions
  • Closures
  • Only one call back at a time
  • I/O through DOM event callbacks

Now let’s talk about the execution of Node JS application with Event Loop. Below is an image taken from internet to explain how Node JS application code gets processed in Event Loop.

This image is taken from Internet!

In Node JS, we have LIBUV software library using which we can perform asynchronous I/O operation. LIBUV offers Event Loop. As you can see, the application code will first get compiled into Native code using Google Chrome’s V8 engine. If needed, it will use Node JS bindings [APIs] and these operations will get placed in Event Queue. If the operation is synchronous, it will get processed in execution stack and the result will be returned immediately. However, if the operation is asynchronous, it will get passed to one of the thread from worker thread pool and stack gets unwind. The second operation will be placed from the event queue into execution stack. The worker thread will process the job in background and eventually we will hear back from our worker thread that the result is ready. This result will get placed in execution stack once the stack is empty and the result will be returned back. This is how Node JS application gets processed.

I hope you understood the Node JS execution stack. That will be all for this article.


Did you find this article useful? Share It!

1 thought on “Why Node JS?

Leave a Reply

Your email address will not be published. Required fields are marked *