Rehan Sattar
Rehan Sattar

Rehan Sattar

How does the V8 Engine actually work?

How does the V8 Engine actually work?

A high-level overview of JavaScript execution in V8 Engine

Rehan Sattar
·Jun 29, 2021·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

What is a JavaScript Engine?

A JavaScript engine is a computer program that executes the JavaScript code you write. It translates your code into what computers understand. There are many JavaScript Engines out there and typically they are created by web browser vendors. All engines are standardized by ECMA Script or ES.

Brief history about JavaScript Engines

The first JavaScript engine was written by Brendan Eich, the creator of JavaScript, in 1995 for the Netscape Navigator web browser. Originally, the JavaScript engine only consisted of an interpreter. This later evolved into the SpiderMonkey engine, still used by the Firefox browser.

In 2008, Google created the Chrome V8 Engine. The V8 engine is an open-source high-performance JavaScript engine, written in C++ and used in the Chrome browser as well as in Node JS. The performance outmatched any engine that came before it mainly because it combines 2 parts of the engine, the interpreter and the compiler. Today, all major engines use this same technique.

Working of V8 Engine

V8.png

1. Parsing Phase

Parsing is the process of analyzing the source code, checking it for errors, and breaking it up into parts.

2. AST Conversion

The parser produces a data structure called the Abstract Syntax Tree or AST. AST is a tree graph of the source code. It does not show every detail of the original syntax but contains structural details.

Example of AST

image.png

You can play with the AST here

3. Interpreter

An interpreter executes each line of code line by line, without requiring them to be compiled into a machine language program. Interpreters can use different strategies to increase performance. They can parse the source code and execute it immediately, translate it into more efficient machine code, execute precompiled code made by a compiler, or some combination of these. In the V8 engine, the interpreter outputs bytecode.

4. Profiler

In modern engines, the interpreter starts reading the code line by line while the profiler watches for frequently used code and flags then passes it to the compiler to be optimized.

5. Compilation Phase

The compiler works ahead of time to convert instructions into a machine-code or lower-level form so that they can be executed by a computer. It runs all of the code and tries to figure out what the code does and then compiles it down into another language that is easier for the computer to read.

Conclusion

First, JavaScript code is parsed and converted to an AST. Secondly, it is interpreted. Then the profile watches and stores used to code and flags. Then JavaScript engine takes the bytecode of the interpreter outputs and mixes in the optimized code the compiler outputs and then gives that to the computer. This is called Just in Time or JIT Compiler.


That's it, folks! hope it was a good read for you. Thank you! ✨

👉 Follow me: Github Twitter LinkedIn Youtube

Did you find this article valuable?

Support Rehan Sattar by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this