How the JavaScript V8 Engine of Chrome Works

Posted By : Arun Singh | 24-Jun-2019

                                   

What is the Javascript Engine

When we write code so you write using the human-readable syntax like using alpha-bates and numbers but that is kind of language human understand. JavaScript engine that piece of software it takes the human-readable code and converts it to low-level machine-understandable representation which done executed by the CPU of the machine. 

 

Popular JavaScript Engines 

V8:- It chrome JavaScript engine, open source, developed by Google, written in C++.
Rhino:- This engine managed by the Mozilla Foundation, open source, developed entirely in Java.
SpiderMonkey:- It was the first JavaScript engine, which back in the days powered Netscape Navigator, and today powers Firefox.
JavaScriptCore:- It also open source, marketed as Nitro and developed by Apple for Safari.
KJS:- KDE’s engine originally developed by Harri Porten for the KDE project’s Konqueror web browser.
Chakra (JScript9):- This JavaScript engine only for Internet Explorer.
JerryScript:- It is a lightweight engine for the Internet of Things.

 

Chrome V8 Engine

All engines are programs V8 that is written in c++. This c++ code one has core functionality to convert the JavaScript code into optimized machine code so that it runs on the host machine in an efficient way. Now earlier JavaScript basically interpreters which worked on the code line by line passing interpreters each of them requiring no compilation to lower level. But V8 employees something calls 'just in time' or JIT compilation. V8 engine used in node run time as well as chrome V8. 

 

JavaScript Engine History

JavaScript Engine was mostly an interpreted language it means there was no compiling process. Every single line is interpreted and executed one after that other. This is a bad way to do stuff.

 

The V8 way of doing Stuff

First of all the dedicated two separate threads for performing compiler related operations. There are several other threads that helps the compilation process but these two other mains once full-codegen and crankshaft. 

 

Main Thread :  Which is suggest the main task fetching the code making sure it compiles and then executing it. 

Compiler Thread: This thread contains two thread once full codegen and crankshaft

  Full-codegen - The function of full codegen was to consume javascript and compile it just before execution google call this 'just in time' or JIT compiler method. When full codegen worked on lines of code and converted into machine code another monitoring thread in browser monitored the code being complied for hot segments that is segments of code that were being hit multiple times. The code inside for-loop for instance classifieds as hot code segments. 
  Crankshaft    - Crankshaft took the codes marked as hot and then compile them in a more optimal way so that the next time that portion of the code was hit this new optimize machine code executed leading the better efficiency.

Profile Thread: Marks the portion of code that is in hot sections.

Garbage Collector Thread: This thread manages the all garbage collection code.

Code Optimization  

The crankshaft applies the so that we get even more efficient compilation.

Inling optimization: fill the function calls in the "hot paths" are replaced with the actual code from the function to improve performance.
Hidden classes: The compiler generates something called hidden classes which maintain the pointers to every object property in a  class structure so that it need not be looked up every time.
Inline caching: A cache of the object type that is most likely to be passed to a function is maintained so that unnecessary lookups are avoided every time.

                                                          

Hot Swapping of code

Full-codegen transforms the javascript abstract syntax tree into what is called static single state assignments its called hydrogen graph. The hydrogen graph optimized the code and converted into lower level language by crankshaft which is called lithium. This is were the minor entriquses like register allocation and finally this lithium code is converted into machine code.


The javascript engine has to do on stack replacement which is the stack register is swap write on the middle execution which is another memory task, in the end, all the process has been done. 

 

Thanks for reading!

About Author

Author Image
Arun Singh

Arun is a MEAN stack developer. He has a fastest and efficient way of problem solving techniques. He is very good in JavaScript and also have a little bit knowledge of Java and Python.

Request for Proposal

Name is required

Comment is required

Sending message..