Chrome V8 Engine - Javascript Runtime for Node.js

Chrome V8 Engine Javascript Runtime For Node Js cover

Javascript is one of the most popular languages, and one of the reasons that makes is really popular is the fact that it is really diverse and performant. Chrome V8 engine plays an important role in making it so performant.

Let's first understand the program lifecycle from source code to execution.

.js file (source code) ➝ compiled into machine code (based on the CPU it runs)

The system that compiles the javascript files into machine code and also manage the program's memory is called javascript runtime.

Chrome V8 engine originally ran in Chrome web browsers. But later it was used to run the javascript without a browser and that exactly what Node.js is. Node.js is written in C++ and build on top of the Chrome V8 engine which is also written in C++. V8 provides the runtime for the Javascript.

How is Javascript compiled and Run?

Javascript Program (source code) ➝ Chome V8 Engine (compiles) ➝ Machine Code (X86_64, MIPS, ARM, etc) ➝ Runs on CPU

Different CPU architecture requires different machine code based on its architecture. So, the program needs to be converted into the machine code that it can understand. Javascript program is compiled and the bytecode (machine code) is optimized and finally, the machine code runs on the CPU. All these operations are performed by the V8.

Chrome V8 engine is really fast because it is written in C++.

Let's see where the Javascript resides based on the closeness to the computer hardware.

Javascript > C/C++ > Assembly Language > Machine Code

Javascript is more high level than C/C++ and C/C++ is more than the Assembly language. Closer the language to the machine better is its performance and less is its compilation complexity.

Main features of the Chrome V8 engine

This engine has the implementation of the ECMAScript, which is the specification of the Javascript language features. For example, it specifies how the function will be defined and how the variable will be declared. V8 is written to understand these specifications to compile and run the javascript.

We can add our own C++ code by wrapping the V8 engine. This allows to add custom functionality to the Javascript. For example, we can add a function print (which is not availble in Javascript by default) using our own C++ code that V8 will be able to understand and compile.

It also manages the memory allocation for the Javascript programs. Allocating and deallocating memory to the objects as well as managing the threads stack memory is the responsibility of the V8 engine. The gabage collection is implemented in the V8 engine, whose responsibility is to remove the unreferenced objects from the memory and also manage the heap space for efficient object creation.

So, now we know what is Chrome V8 engine and the roles played by it in the Node.js for making Javascript really fast and performant.

Link to the part 1 of this article series: https://janisharali.com/blog/introduction-to-node-js-and-how-node-js-works

Video lecture on Chome V8 Engine