Node.js – Introduction, V8 Engine & Internal Working

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

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.

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.

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

  • Asynchronous and Event-driven I/O: It handles concurrent requests easily. Node.js runs asynchronously i.e. in a non-blocking manner. So Node.js based server never waits for an API to return data. The server moves to the next API calls. The notification mechanism of Events of Node.js helps the server to get a response from the previous API call. So that is why it is very fast.
  • Extremely fast: Node.js is built on Google Chrome’s V8 JavaScript Engine, so its library is very fast in code execution.
  • Single-threaded: Node.js follows a single-threaded model with event looping.
  • No buffering: Node.js cuts down the overall processing time while uploading audio and video files. Node.js applications never buffer any data. These applications simply output the data in chunks.

Node.js can be used to develop

  • Chat Applications
  • Game Servers
  • Streaming Applications
  • Data-Intensive Real-time Applications
  • JSON-based APIs (mostly used)
  • Single Page Applications

Don’t use Node.js for

  • CPU intensive applications
  • Basic CRUD applications
  • Applications using RDBMS