AfterAcademy Tech
•
29 Oct 2019

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.
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.
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://afteracademy.com/blog/introduction-to-node-js-and-how-node-js-works
AfterAcademy Tech
In this article we will explore the core structures of the Node.js architecture. Node.js is a popular framework for building backend systems (network applications) which can scale very well. Node.js is a Javascript runtime based on Chrome V8 engine.

AfterAcademy Tech
Most programming languages have their own debuggers supported by IDEs but Javascript is a scripting language, therefore, it doesn’t have an inbuilt full-featured debugger. We will discuss more on how to debug Node.js Applications.

AfterAcademy Tech
Node.js uses the event-loop and callback mechanism to lift off I/O operations from the javascript's thread to the system's kernel. And in this article we are going to understand how this actually happens.

AfterAcademy Tech
In this tutorial, we are going to learn how to upload files on the server with the help of Multer and Express in Node.js. The goal is to make you comfortable in building apps that can easily handle any file uploads. At the end of this blog, you will be able to integrate the file uploads in your own apps.
