TOUSEEF
Node.js: Introduction, V8 Engine & Internal Working

Node.js: Introduction, V8 Engine & Internal Working

August 28, 2022
|
Node.js

What is node.js

It is an open-source and cross-platform JavaScript runtime environment used for the server-side development of applications.

It is built on Chrome’s V8 Engine so that JavaScript can run out of the browser.

Simplifying the things:

As we know JavaScript is limited to running inside of our web browsers as a client-side language only. Node.js creates an environment for us to run JavaScript out of the browser i.e. on the server. And for doing so, it uses Chrome’s V8 Engine.

Node.js is called the run-time environment because it contains everything that you need to run JavaScript on the server side.

Node.js = Runtime Environment + JavaScript Library

node environment Before continuing further, let us get familiar with the V8 Engine.

Chrome V8 Engine

It is an open-source JavaScript Engine developed by Google and written in C++.

V8 Engine is an interpreter that was originally developed to create an environment to run JavaScript in Chrome and Chromium-based browsers (e.g. Brave, Edge, Opera, etc.).

The V8 engine is completely independent of the browser which is why it not only runs on Chrome but also on other browsers.

Electron JS, Deno, and MongoDB also use the V8 Engine.

” It is the heart of Node.js “

How Node was born

The developer of Node.js replicated the browser environment outside of an actual browser by using V8 Engine and hence node.js was born.

Node JS = V8 Engine + Node API & Modules

Here V8 Engine can be referred as Runtime environment & Node API & modules as libraries.

How Node.js works internally

The “Single Threaded Event Loop” architecture is used by node.js to handle multiple concurrent requests. Its working model is based on the JavaScript event-based model along with the callback mechanism.

Node js server comprises of event queue, event loop, and thread pool.

Some terminologies that you first need to understand:

Requests: These are the tasks that user performs in web apps which can be blocking (complex request that takes time to complete) or non-blocking (simple requests).

Event Queue: It is the place where the Node.js server stores incoming requests and these requests are passed to the Event Loop.

Thread Pool: It consists of all the threads available for carrying out blocking tasks i.e., the tasks that can block the server are handled by the thread pool.

Event Loop: It constantly watches for events in the event queue that needs to be executed asynchronously. It executes the asynchronous requests and then calls a callback function when the task is completed.

nodejs working

Each request/job comes into the event queue and is handled by a single thread and complex operations are done asynchronously. The asynchronous requests are done in the thread pool. As the previous asynchronous request is completed then the response or callback is sent back to the client and within this time period, new requests can also be managed.

Because of asynchronous & non-blocking behavior, no request has to wait for the thread to be free. And also because of using a single thread, fewer resources are used.

Each asynchronous request is managed by Event Loop. The event loop constantly watches the event queue for events that need to be raised for the async job and sends them to the thread pool and the thread pool returns the callback function when the job is completed.

Node.js uses “libuv” for the event loop which in turn uses internal C++ “thread pool” to provide async I/O.

Traditional Server Process Model

In a traditional server, each request is handled by a dedicated thread from the thread pool. Each dedicated thread executes a request and does not return to the “thread pool” until it completes execution and returns a response for that request.

If no thread is available in the thread pool then the request will wait till a thread is available.

traditional server working

Asynchronous (Node.js) vs Synchronous (PHP or ASP) Code Execution

For example, if there is a task to read a file,

What node Node.js does is

It sends the task to the computer’s file system → Gets Ready to handle the next task → When the file system has opened and read the file, the server returns the content to the client

What PHP or ASP does is

It sends the task to the computer’s file system → Waits while the file system opens and reads the file → Returns the content to the client → Ready to handle the next request.

Features of Node.js

Node.js can be used to develop

Don’t use Node.js for