Overview on Node.js
Node.js came into existence when its creator Ryan Dahl, understanding the power of V8, powered the Chrome browser and extended it so that it can run on your machine as a standalone application.
Another part of the definition on the official Node.js website says,Node.js uses an event driven, non-blocking I/O model that makes it lightweight and efficient.
I/O refers to input/output and this is where the additional functionality of Node.js comes into play. We can read and edit local files in Node.js and also do an HTTP request to an API.
The earlier backend systems like PHP and ASP used to block the program till a network request was complete. But it was completely changed by Node.JS, which sends the request and then goes to the next line of code. So, it is non-blocking and faster than the earlier backend technologies.
But it is a single-threaded technology and that is where it has some limitations, whereas Java shines because of it being multi-threaded.
Yet another part of the official definition on the Node.js website says,Node.js package ecosystem, npm is the largest ecosystem of open-source libraries in the world.
Over the past decade, an amazing community of open-source enthusiasts have created more than 1 million npm packages, which enhance the capabilities of Node.js.
It is completely open-source and anyone can use it, as it has an MIT licence, for developing server-side and networking applications. It can run on all three Operating Systems i.e., Mac OS, Windows, and Linux.
How V8 Engine works
The V8 engine also uses some other components, along with the ones we had seen above. They are Liftoff and Orinoco–
- Liftoff is responsible for machine code generation in a highly optimized way. It generates code for each opcode and perform way better then Turbofan.
- Orinoco is responsible for garbage collection. It looks for disconnected memory allocations and perform operations to free up more space. It also update the pointers to new memory locations.
V8 also uses a lot of different threads and they are –
- There is another thread which is used to optimize the running code, while the primary thread continues its execution.
- Yet another thread is used for profiling, which tells on runtime the methods that are needed to be optimized.
- Some of the threads also do garbage collection.
The Just-in-Time Paradigm
We will learn a bit more about the Just-in-Time (JIT) compilation in V8. For a code to execute in any programming language, it must be converted into machine code, which the computer understands. There is a different paradigm for this transformation.
g++ -o mycppprogram mycppprogram.cpp
This converts the code into machine code after which we can run our program with commands like below.
To overcome this problem in dynamic languages, the approach of Just-in-Time (JIT) compilation, was created, which combines the best of both interpretation and compilation. So, an interpretation step runs before the compilation step, where the V8 engine detects the more frequently used functions and code and compiles them using information from previous executions.
During compile time, this code is re-compiled for optimal performance.
What is the relationship between Node and V8?
The core powering Node.js is this V8 engine. The diagram shows a comparison with the Java Virtual Machine (JVM), which power the Java Runtime environment. Beside the V8 engine the Node.js runtime environment adds many Node APIs to power the Node.js environment. We can also extend the functionality of our node code by installing additional npm packages.
Can Node.js work without V8?