NodeJS Core

NodeJS is an asynchronous event-driven JavaScript runtime environment built on Chrome’s V8 engine with non-blocking I/O, it's designed to build scalable network applications.

NodeJS performance

Chrome’s V8 engine, the fastest javascript engine, significantly improves Node.js performance. Node.js is single-threaded. asynchronous, and works on a non-blocking I/O model. This boosts performance by handling multiple concurrent threads simultaneously with fewer resources.

The main reason Node.js is so fast is due to its non-blocking I/O model. NodeJS uses a single asynchronous thread, which reduces the CPU workload and avoids HTTP congestion.

Web applications powered by Node.js benefit massively from its ability to multitask. Unlike other platforms, its single-threaded, event-driven architecture processes multiple concurrent requests efficiently without clogging the RAM. Moreover, its event-loop and non-blocking I/O operations allow code execution at a pace that significantly impacts the application’s overall performance.

NodeJS Pros and Cons

Pros:

Cons:

NodeJS Architecture

Event Loop

As you can see in the following diagram, Node starts the event loop by checking for any expired timers in the timers queue, and go through each queue in each step while maintaining a reference counter of total items to be processed. After processing the close handlers queue, if there are no items to be processed in any queue and there are no pending operations, the loop will exit. The processing of each queue in the event loop can be considered as a phase of the event loop.

What’s interesting about the intermediate queues depicted in red is that, as soon as one phase is complete event loop will check these two intermediate queues for any available items. If there are any items available in the intermediate queues, the event loop will immediately start processing them until the two immediate queues are emptied. Once they are empty, the event loop will continue to the next phase.

Streams

Stream is a sequence of data elements made available over time

In Node JS streams are:

  • Readable Streams - can be used to read data from an underlying source such as a file descriptor. Data can be stored in a Buffer within the readable stream if the application consumes slower than the operating system reads from the source.

  • Writable Streams - A writable stream is used to write data from an application to a certain destination. To prevent data loss or overwhelming of the destination in case the destination is slower than the writing application, the data may be stored in an internal Buffer.

  • Duplex Streams - is a hybrid of a Readable stream and a Writable stream. An application connected to a duplex stream can both read from and write to the duplex stream. The most common example of a duplex stream is net.Socket. In a duplex stream, read and write parts are independent and have their own buffers.

  • Transform Streams - is an even special hybrid, where the Readable part is connected to the Writable part in some way. A common example would be a crypto stream created using Cipher class. In this case, the application writes the plain data into the stream and reads encrypted data from the same stream.

Piping Streams

In many cases, streams are even more useful when connected together. As obvious as it may sound, this is called ‘piping’. You can connect a Readable stream to another Writable/Duplex or a Transform stream by using the Readable stream’s pipe() method.

Last updated

Was this helpful?