Nodejs - Overview
I. What is Nodejs ?
Nodejs is an open source, cross - platform Javacript runtime enviroment. it is used as backend service where javascript works on server -side of application.
II. How does it work?
There are three main components we must understand to see how Node.js works. These components are:
V8 Engine
- Node.js run on chrome v8 engine which convert javascript code into mechine code.
- 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. Whenever the engine is executing code and comes across any of those variables, it looks up the actual value from the memory heap.
- 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.
- One thread == one call stack == one thing at a time.
- Is a C library, used for performing Input/output (I/O) operations. This give Node access to the underlying computer operation system, file system and network.
- I/O operations have to do with sending requests to the computer and receiving responses. These operations include reading and writing files, making network requests, and so on.
- Libuv is cross-platform (can run on any operating system) and has a focus on Asynchronous I/O. It can handle more than one I/O operation at once.
- Libuv executes the I/O operation: Whenever we pass a script to Node.js, the V8 engine parses the code and starts processing it. The call stack holds the invoked functions and keeps track of the program. If the V8 engine comes across an I/O operation, it passes that operation over to Libuv.
- One interesting thing to note is that it is true that JavaScript is a single-threaded language, but Libuv—the low-level library Node.js uses— can make use of a thread pool (multiple threads) when executing instructions in the operating system.
- When we run our JavaScript program that contains some asynchronous code (like I/O instructions or timer-based actions), Node.js handles them using the Node.js APIs. Asynchronous functions usually have instructions to be executed after the function has finished processing. Those instructions are placed in a Callback Queue.
- 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.
- As the event loop runs, it checks if the call stack is empty. If the call stack is not empty, it allows the ongoing process to continue. But if the call stack is empty, it sends the first instruction on the callback queue to the JavaScript engine. The engine then places that instruction (function) on the call stack and executes it.
- The callback queue is a software mechanism that stores callback functions to be run after the Web APIs have processed asynchronous functions.
Comments
Post a Comment