Go deep in NodeJS - Part 2
Node.js

Go deep in NodeJS - Part 2

V8 is the JavaScript engine that was Google's open-source project. It was developed for running JavaScript codes in the Chrome browser and in 2009 it was chosen as the Node.js engine.

Other browsers have their engine like Firefox has SpiderMonkey, Safari has JavaScriptCore (also called Nitro), and Edge has recently been rebuilt using Chromium and the V8 engine.

We mention some key features of V8 here.

  • V8 is written in C++ and also it compiles JavaScript code into machine code. (It does not interpret it).
  • V8 has memory management and garbage collection for allocation and deallocation.
  • It is designed to work in cross-platform including Windows, Linux, macOS, and mobile platforms.


How V8 works

There are some steps.

  • V8 first parses the JavaScript code into an abstract syntax tree (AST).
  • JavaScript is internally compiled by V8 with JIT compiler.
  • The machine code works directly with the computer processor, so it does not require much time to complete the operations.
  • As the code runs, V8 has a monitoring role to play in the performance of the code. It determines which parts of the program are called most often (hot code) and enhances these parts to be even faster the next time they are called. Refactoring may involve recompiling to improve the efficiency of some of the code portions as required.
  • It is equally important to note that V8 comes with its own garbage collectors to help in managing the memory. It releases memory that a particular program or function does not require any longer and thus prevents memory leaks.


Here is an example of the process of JavaScript function by V8:

function add(a, b) {
    return a + b;
}
console.log(add(2, 3));        

  • Parsing:

V8 reads the JavaScript code, breaks it down into tokens and It parses into AST.

FunctionDeclaration
  Identifier (name: "add")
  Parameters
    Identifier (name: "a")
    Identifier (name: "b")
  BlockStatement
    ReturnStatement
      BinaryExpression (operator: "+")
        Identifier (name: "a")
        Identifier (name: "b")        

  • Compilation:

V8 uses a JIT compiler to convert the AST into machine code.

Bytecode
  Load a
  Load b
  Add
  Return        

  • Execution:

The computer’s processor executes the machine code.

add(2, 3);        

  • Optimization:

As the code runs, V8 monitors the performance of the `add` function.

If the function is called frequently, V8 identifies it as a "hot code."

Optimized Bytecode
  Load a
  Load b
  Add (optimized)
  Return        

  • Garbage Collection:

V8 manages memory automatically. After the `add` function executes, the garbage collector frees up the memory if there are no more references to its variables (a and b).

Memory Management
  Free memory used by a and b        

All engines implement ECMAScript, the standard used by JavaScript, which we talk about in more detail in the next section.


Conclusion

V8 JavaScript Engine is one of the most important and performant engines as it is used in web browsers like Google Chrome and on the server side like Node.js. undefined Due to such factors as its capacity to translate JavaScript to machine language, efficient memory management as well as optimization, it has become a vital component for advanced web design.

All engines implement ECMAScript, the standard used by JavaScript, which we talk about in more detail in the next section.


Mohsen Shafie

Nodejs developer,Full-stack developer, Technical team leader, Software Developer, CTO

8 个月

???? ???

要查看或添加评论,请登录

Amir Kenarang的更多文章

  • Go deep in NodeJS - Part 4

    Go deep in NodeJS - Part 4

    This post inspired by javascripttutorial An execution context is like a box where your JavaScript code runs. It keeps…

  • Go deep in NodeJS - Part 3

    Go deep in NodeJS - Part 3

    ECMAScript ECMAScript is a standard for scripting languages, including JavaScript. It is best known as a JavaScript…

  • Go deep in NodeJS - Part 1

    Go deep in NodeJS - Part 1

    Welcome to the journey of learning Node.js! No matter whether you are a first-timer or a ninja-level developer, this…

社区洞察

其他会员也浏览了