In synchronous I/O, the thread (a thread is just a sequence of instructions) will wait till the entire operation is finished. On the other hand, in Asynchronous I/O, the line does not wait during the operations. The operation will run in the background, and it will be called when it is finished. The Asynchronous I/O feature enables an application to have more CPU time available to perform other processing while the I/O is taking place.
LIBUV
LIBUV is another significant dependency on node.js. It gives node.js access to the machine operating system, networking, file system, and more. LIBUV is an open-source library with a strong focus on asynchronous I/O (Input-output). LIBUV is written in C programming language. Apart from focusing on the asynchronous I/O, LIBUV also implements two essential features: event loop and thread pool.
LIBUV is another significant dependency on node.js. It gives node.js access to the machine operating system, networking, file system, and more. LIBUV is an open-source library with a strong focus on asynchronous I/O (Input-output). LIBUV is written in C programming language. Apart from focusing on the asynchronous I/O, LIBUV also implements two essential features: event loop and thread pool.
You should remember that the Event Loop is the heart of Node js, which makes the node completely different from other backend languages.
Other Important Libraries
The most critical libraries for Node.js are LIBUV and V8. However, the node is not only based on V8 and LIBUV but also on a few other libraries like HTTP parser for parsing HTTP, C-ARES for DNS queries, OpenSSL for cryptography, and Zlib for file compression. When all of these components come together ideally, we end up with Node.JS ready to be used on the server-side for all of our applications.
The most critical libraries for Node.js are LIBUV and V8. However, the node is not only based on V8 and LIBUV but also on a few other libraries like HTTP parser for parsing HTTP, C-ARES for DNS queries, OpenSSL for cryptography, and Zlib for file compression. When all of these components come together ideally, we end up with Node.JS ready to be used on the server-side for all of our applications.
Event-Driven Architecture
Most of the node's core modules, like HTTP File System, are built around an event-driven architecture. The concept is quite simple. In node, there are particular objects called event emitters that emit named events as soon as something important happens in the app, like a request hitting server or a file finishing to read. Event listeners then pick up these events that we developers set up, which will fire off functions(callback functions) attached to each listener.
Most of the node's core modules, like HTTP File System, are built around an event-driven architecture. The concept is quite simple. In node, there are particular objects called event emitters that emit named events as soon as something important happens in the app, like a request hitting server or a file finishing to read. Event listeners then pick up these events that we developers set up, which will fire off functions(callback functions) attached to each listener.
Non Blocking I/O Model: The main thread of Node.js assigns time-consuming asynchronous operations to background threads. Instead of waiting for these operations to finish, the main thread proceeds to handle the subsequent request in the event queue. This allows Node.js to efficiently manage I/O operations without blocking the main thread. Once the background thread finishes the asynchronous task, it notifies the main thread to take up the operation for further execution of the callback code. This ensures that the main thread remains unblocked while the I/O operation is carried out asynchronously.
Brendan Eich, who worked for Netscape, invented JavaScript in 1995. But it was a programming language that could only run on a browser.
In 2008, Google announced a new Web Browser called Chrome. This browser when released revolutionized the world of internet browsing. It's an optimized browser that executes JavaScript fast and has improved the user experience on the web.
The reason Google Chrome could execute JavaScript code so fast was that a JavaScript engine called V8 ran inside Chrome. That engine was responsible for accepting JavaScript code, optimizing the code, then executing it on the computer.
The engine was a proper solution for client-side JavaScript. Google Chrome became the leading Web Browser.
In 2009, a software engineer named Ryan Dahl criticized the popular way back-end servers were run at the time. The most popular software for building Web Servers was the Apache HTTP Server. Dahl argued that it was limited, in that it could not handle a large number of real-time user connections (10,000 +) effectively.
This was one of the main reasons that Ryan Dahl developed Node.js, a tool he built. Node.js used Google’s V8 engine to understand and execute JavaScript code outside the browser. It was a program whose purpose was to run Web Servers.
Node.js was a great alternative to the traditional Apache HTTP server and slowly gained acceptance among the developer community.
The reason Google Chrome could execute JavaScript code so fast was that a JavaScript engine called V8 ran inside Chrome. That engine was responsible for accepting JavaScript code, optimizing the code, then executing it on the computer.
The engine was a proper solution for client-side JavaScript. Google Chrome became the leading Web Browser.
In 2009, a software engineer named Ryan Dahl criticized the popular way back-end servers were run at the time. The most popular software for building Web Servers was the Apache HTTP Server. Dahl argued that it was limited, in that it could not handle a large number of real-time user connections (10,000 +) effectively.
This was one of the main reasons that Ryan Dahl developed Node.js, a tool he built. Node.js used Google’s V8 engine to understand and execute JavaScript code outside the browser. It was a program whose purpose was to run Web Servers.
Node.js was a great alternative to the traditional Apache HTTP server and slowly gained acceptance among the developer community.
What is a runtime environment?
The Runtime Environment of a programming language is any environment where a user can execute code written in that language. That environment provides all the tools and resources necessary for running the code. Node.js is a JavaScript runtime environment.
The Runtime Environment of a programming language is any environment where a user can execute code written in that language. That environment provides all the tools and resources necessary for running the code. Node.js is a JavaScript runtime environment.
The V8 engine contains a memory heap and call stack. They are the building blocks for the V8 engine. They help manage the execution of JavaScript code.
The memory heap is the data store of the V8 engine. Whenever we create a variable that holds an object or function in JavaScript, the engine saves that value in the memory heap. To keep things simple, it is similar to a backpack that stores supplies for a hiker.
Whenever the engine is executing code and comes across any of those variables, it looks up the actual value from the memory heap – just like whenever a hiker is feeling cold and wants to start a fire, they can look into their backpack for a lighter.
The memory heap is the data store of the V8 engine. Whenever we create a variable that holds an object or function in JavaScript, the engine saves that value in the memory heap. To keep things simple, it is similar to a backpack that stores supplies for a hiker.
Whenever the engine is executing code and comes across any of those variables, it looks up the actual value from the memory heap – just like whenever a hiker is feeling cold and wants to start a fire, they can look into their backpack for a lighter.
The call stack is another building block in the V8 engine. It is a data structure that manages the order of functions to be executed. Whenever the program invokes a function, the function is placed on the call stack and can only leave the stack when the engine has handled that function.
JavaScript is a single-threaded language, which means that it can only execute one instruction at a time. Since the call stack contains the order of instructions to be executed, it means that the JavaScript engine has just one order, one call stack.
The Callback Queue works with the First In First Out (FIFO) approach. That means the first instruction (callback) to enter the queue is the first to be invoked.