Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 20, 2022 02:47 am GMT

A Look Under the Hood of Node.JS

There are many technologies that excel in real-time web applications where both the client and server can initiate communication, and where data is exchanged freely. Node.JS is just one of those innovative technologies designed to build scalable network applications. It does this with its unique makeup of dependencies built in C++ along with an infrastructure that handles event looping.
Underneath the hood we find that Node.JS utilizes two main dependencies: V8 JavaScript Engine and LibUV. V8 is Google's open source high-performance JavaScript and WebAssembly engine that converts JavaScript into machine code that a computer can actually understand, but V8 alone is not enough to create a server-like framework like Node. LibUV, an open-sourced library, adds file system and networking steps functionality. One thing to note is that both these libraries are written in C++, not Javascript. As developers, Node.JS provides us a layer of abstraction for event-driven and object-oriented programming, without having to worry about these low-level considerations.
Node.JS is an event-driven architecture built all around callback functions, which are like instructions that are called and executed as soon as some work is finished in the future. In the real world, this can be as simple as someone clicking a button in an application and having that event follow with a new page uploaded with necessary data. Other events that trigger callback functions commonly include servers receiving HTTP requests, timers expiring, or the termination of file reading processes at the operating system level. These actions emit events as soon they are done with their work, all handled within the Event Loop.
But what exactly is the event loop? According to Node.JS' own website, The event loop is a constantly running process that monitors both the callback queue and the call stack. If the call stack is not empty, the event loop waits until it is empty and places the next function from the callback queue to the call stack. In other words, the event loop orchestrates: 1) receiving events, 2) calling the necessary callback functions, and 3) allocating tasks to the thread pool. All code within callback functions is executed when the callback is triggered (not when the main program runs and enqueues the callbacks).

The sequence is as follows:

Callbacks enqueued -> Events emitted by APIs/detected by Event Loop -> Callbacks called

At the heart of the Node.JS architecture, we find that the event loop runs within a single-threaded process, which is different from operations taking place within a multi-threaded process. What does this mean? Well for starters, a multi-threaded process involves execution of programmed instructions in more than a single sequence. With this framework, instructions wont have to wait to execute unless multiple instructions are grouped within different sequences. You may be thinking to yourself How does a single-threaded process deliver more efficiency than one that is multi-threaded? This is where the Thread Pool comes into play.

The Thread Pool is a set of available operating system threads (a dedicated memory space on the operating system for performing code execution). The event loop is the single thread whose job it is to monitor events and hand off workloads (callbacks as they are triggered by events) to available threads. The event loop itself must always be running, constantly checking (synchronously) for work that needs doing and threads that can do it.


Original Link: https://dev.to/velouriagreen/a-look-under-the-hood-of-nodejs-1b4a

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To