Open In App

What is the relationship between Node.js and V8 ?

Last Updated : 02 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment built on the V8 engine and executes the Javascript code outside the browser. A Javascript runtime environment contains all the components needed to execute the JS code. JavaScript was initially designed to run only in the browser. All browsers are equipped with JavaScript engines for running JavaScript. For example, Mozilla Firefox is powered by Spidermonkey, while Chrome uses the V8 engine. JavaScript engines are not machines but rather programs or interpreters that execute JavaScript code and enable computers to perform specific tasks at the hardware level.  Ryan Dahl created Node.js when he realized the potential of the V8 engine in the Chrome browser and expanded it to run as a standalone application. Computers can only interpret machine code, and Javascript is a high-level language, so we need a compiler to translate Javascript to machine code. Node.js is just a runtime environment that supports the execution of a program. V8 is at the core of Node.js.

Node.js cannot run without V8. Since there wouldn’t be a JavaScript engine, no JS can run in such an environment. The Chrome V8 engine takes the human-readable Javascript code and translates it into more optimized machine code. It is an open-source multithreaded program written in C++. The engine was initially designed solely for execution by web browsers, but the latest versions also execute JS code outside of the browser, enabling server-side scripting. It is independent of the browser in which it executes. This feature inspired the designers to opt for the V8 engine to power Node.js. Node.js is written in C++. The V8 engine is embedded inside node.js code, which adds additional features to the code. As a result, the C++ code understands more than the ECMAScript standard otherwise specifies. V8 engine and node.js are interoperable.  Together, they make a powerful combination for developing applications. The V8 engine is the heart of Node.js.  

Working of V8 engine: Js Engines were earlier interpreters, executing code line by line, requiring no compilation, making the process very slow as each statement is analyzed before execution to perform the desired task.  As V8 came along, it solved this problem by using separate threads to speed up the compilation process. V8 employs a technique of the Just in Time(JIT) compiler. Just-in-time compilers combine the best features of interpreters and compilers, making both translation and execution faster. The interpreter executes the code line by line and the compiled code is additionally optimized (and re-optimized) dynamically at runtime. Now, the question is whether Javascript is an interpreted or compiled language. Javascript can either be an interpreted or a compiled language, depending on the JS engine.  

V8 uses the following threads.  

1. Main Thread: The FETCH thread performs the main task of fetching the code and ensuring it is being compiled and executed.

2. Compiler Threads: The previous versions of V8 used two threads for compilation.

  • The Full-codegen: This compiles the JS into simple and relatively slow machine code. With full-codegen, parsed JavaScript is translated directly into machine code without any transformations. Therefore, it is very fast to execute machine code.
  • Crankshaft: It compiles segments of code that run multiple times, often called hot sections, optimally, to execute them more efficiently next time when this portion of the code is hit.

This thread undergoes certain optimizations.  

  • Inlining: A function’s call is replaced with the code of the function, thus improving performance.
  • Hidden Classes: JavaScript is a dynamic language, so properties can be added or removed easily. Keeping track of every property becomes expensive therefore, it uses the method of hidden classes. A transition path to a new hidden class is added to each object’s old hidden class when a new property is added.
  • Inline Caching: V8 maintains a cache of the types of objects that were passed as parameters in recent method calls, and uses that data to guess what type of objects will be passed in the future.

3.  Profiler Threads: Informs the runtime which methods take most of our time, so Crankshaft can optimize them

4. Garbage Threads: Releases space for memory management using the mark and sweep algorithm

Let’s see how the V8 engine executes JS code written inside Node.js

Execution of Javascript in node.js 

1. Parsing: Javascript source code is parsed and converted to the Abstract syntax tree (AST). The code is broken down into tokens. Token is the building block of our code, anything that makes up the code. For example, let a = 5. Here let, a, =, 5 are tokens.

let a = 5 

 Ast plays a crucial role in the semantic analysis of a program since the compiler verifies the correctness and correct usage of applications and language elements. 

Let’s see the representation of an AST by the following example: 

const blog = "GeeksforGeeks"

AST Representation 

AST Representation in JSON 

{
  "type": "Program",
  "start": 0,
  "end": 28,
  "body": [
    {
      "type": "VariableDeclaration",
      "start": 0,
      "end": 28,
      "declarations": [
        {
          "type": "VariableDeclarator",
          "start": 6,
          "end": 28,
          "id": {
            "type": "Identifier",
            "start": 6,
            "end": 10,
            "name": "blog"
          },
          "init": {
            "type": "Literal",
            "start": 13,
            "end": 28,
            "value": "GeeksforGeeks",
            "raw": "\"GeeksforGeeks\""
          }
        }
      ],
      "kind": "const"
    }
  ],
  "sourceType": "module"
}

2. Compilation: Ignition generates an intermediate code called Byte code from the syntax tree.

3. Execution: Turbofan converts this byte code into highly optimized machine code. The machine code runs on the system.  

So, we have understood the relationship between Node.js and V8 engine and concluded that it is impossible to run Node.js without a JavaScript engine such as V8.  


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads