What is V8 engine?
V8 engine is a JavaScript engine which parse the source code of JavaScript into machine readable code. And the V8 engine is made by Google. They use this engine in Google Chrome Browser. It takes the DOM and other web APIs provided by the browser and execute the source code.
This is independent of browser in which it is hosted. That's why it could be integrated in node.js. In 2009, the V8 engine become so much popular that many server-side code are written using JavaScript within NodeJS environment.
Engine Language
The V8 engine is writen in c++ and it is evolving day by day. It run on Mac, Windows, Linux and other system as well. It became faster with nodejs ecosystem. It follows the ECMAScript Standard.
Other JavaScript Engine
There are some more JavaScript engine as well
- Firefox has SpiderMonkey (which was first JavaScript engine)
- Safari has JavaScriptCore
- Edge had Chakra but now they are using Chromium
Compilation
JavaScript was interpreted language. But now it is typical. Today it is interpreter and compile as well. The idea was first introduced by Firefox in 2009. It is now JIT (Just in Time) compilation to speed up the execution. It Follow 3 phase of compilation.
- Parse JavaScript code
- Compilation
- Execution
Parser
The source code, in first phase, is parsed into AST (Abstract Syntex Tree). There is a simple example of AST of JavaScript code.
let myName = "Torjuman Rimon";
{
"type": "Program",
"start": 0,
"end": 29,
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 29,
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 29,
"id": {
"type": "Identifier",
"start": 4,
"end": 10,
"name": "myName"
},
"init": {
"type": "Literal",
"start": 13,
"end": 29,
"value": "Torjuman Rimon",
"raw": "\"Torjuman Rimon\""
}
}
],
"kind": "let"
}
],
"sourceType": "module"
}
Now we can see just a simple variable with small string need this much of Tree structure.
Compilation
After parsing the source code, it parallelly interpreted and compiled the code to optimize. it first uses ignition to interpreted bytecode, and it was not optimized. In the meantime, there is a compiler call TusrboFan compiler which constantly optimize the byte code.
Execution
In Execution context, we know there are two things memory heap and call stack. All variable and function are host in memory heap and then it goes to call stack pop up from call stack.