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.
How V8 works
There are some steps.
Here is an example of the process of JavaScript function by V8:
function add(a, b) {
return a + b;
}
console.log(add(2, 3));
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")
V8 uses a JIT compiler to convert the AST into machine code.
Bytecode
Load a
Load b
Add
Return
The computer’s processor executes the machine code.
add(2, 3);
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
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.
Nodejs developer,Full-stack developer, Technical team leader, Software Developer, CTO
8 个月???? ???