NA +91-8727899942 thecoderjob@gmail.com

CHAPTER - 2

What is Node JS architecture and event loop concept.

Node.js is a popular runtime environment that allows you to run JavaScript code on the server-side.

Node.js Architecture

  1. V8 Engine: Node.js uses the V8 JavaScript engine, developed by Google, to execute JavaScript code. V8 compiles JavaScript directly to native machine code before execution, which makes it very fast.

  2. Libuv: Libuv is a multi-platform C library that provides event loop and asynchronous I/O capabilities to Node.js. It abstracts the differences between various operating systems and provides a consistent API for asynchronous operations.

  3. Event Loop: The event loop is the heart of Node.js architecture. It continuously checks the event queue for pending events and executes them asynchronously. This allows Node.js to handle many connections concurrently without blocking the execution of other code.

  4. Node.js API: Node.js provides a rich set of built-in modules and APIs for various I/O operations, networking, file system access, and more. These APIs are non-blocking, meaning they allow you to perform asynchronous operations without blocking the event loop.

Event Loop

The event loop is a fundamental concept in Node.js that enables asynchronous I/O operations. Here's how it works:

  1. Event Queue: When an asynchronous operation (like reading a file or making a network request) is initiated, it is offloaded to the system, and a callback function is registered to be executed once the operation is completed. The callback is placed in the event queue.

  2. Event Loop: The event loop continuously checks two main sources - the call stack and the event queue. If the call stack is empty, the event loop will take a callback from the event queue and push it onto the call stack for execution.

  3. Non-blocking: Because the event loop operates asynchronously, it can handle multiple concurrent operations efficiently without blocking the main thread. This is what makes Node.js highly scalable and performant for I/O-bound applications.

Here's is the Code !!

console.log("Start");

setTimeout(() => {
    console.log("Timeout callback");
}, 0);

Promise.resolve().then(() => {
    console.log("Promise resolved");
});

console.log("End");

// Output
Start
End
Promise resolved
Timeout callback

Chapter 1

What is Node.js?

Previous chapter

Chapter 3

How to setting up Node JS and NPM

Next chapter

Get In Touch

NA

thecoderjob@gmail.com

+91-8727899942

Popular Links

© www.thecoderjob.com. All Rights Reserved.               Designed by HTML Codex