3

Basics of understanding Chrome’s V8 Engine

 2 years ago
source link: https://medium.com/@duartekevin91/basics-of-understanding-chromes-v8-engine-c5c8ec61fa6b
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In order to understand and appreciate what the Chromes V8 engine is doing first one needs to know what a Javascript engine is.

What is a Javascript engine ?

A JS(Javascript) engine is an interpreter or compiler of JS code into machine code. JS being a higher level dynamic language has no way to directly interact with our machines lower level logic. Not all JS engines are built equally, there are the traditional compilers and more modern ones that utilize just-in-time compilation to bytecode. V8 and most modern JS engines are just-in-time compilers; where the engines differ is in the implementation or pipe-line of the interpreter and optimized compilers. More on optimized compilers later.

These engines for the most part live and operate inside web browsers like Chrome, Firefox, Safari etc. However with the release of NodeJs in 2011 we now can use the V8 engine outside of the web browsers and directly on our machines.

Popular JS engines:

What is a just-in-time compiler ?

A JIT(just-in-time) compiler takes the benefits from both the traditional compiler and an interpreter and mixes them together.

What is a compiler?

A compiler, in short, will run through the entire code base first and execute the code only after it has been translated into machine code or another lower level language. This makes the code more optimized on the machine level however sometimes this compilation can take a long time depending on the size of the code base. For web applications this isn’t practical because we want a smooth and fast user experience.

What is an interpreter?

An interpreter on the other hand will take code as it comes in and translate it into machine code and execute right away. This makes it much faster however the down fall is that it in not efficient for the machine due to more redundant machine code. This has todo with how machine code is written and stored vs. how we write JS or another higher level language.

So the way a JIT handles both of these operations is interesting, but I will try to keep it at a higher level and not get into the nuances of how data is stored in memory. A JIT compiler will run in real time and interpret code and execute it just as an interpreter does however there is an added data collector called the monitor or profiler which looks for the same code that is executed multiple times like a looping function. It then categorizes this code as “warm”, “hot” or “very hot” based on how often the function is executed.

Generally if the code is “warm” or “hot” it will be sent off to a baseline compiler which will compile the code in an efficient way however if the code is “very hot” there is another compiler called the optimized compiler which further optimizes the code. These compilers obviously take some time to compile so there is an initial overhead to compiling these function however in the long run the code will run much more efficient if the JIT pipeline is set up correctly.

What makes the V8 engine special?

The V8 engine is a high performance Javascript engine written in C++. It is an open source project by Google. It also has the flexibility to be used both on the client side and server side(thanks to NodeJS). Essentially it is just like all other Javascript engines however this one has been “tuned” for high performance.

“Recent benchmarks have put its speed ahead of PHP, Ruby and Python. There is hope that it could become as fast as C.”

“When V8 compiles JavaScript code, the parser generates an abstract syntax tree. A syntax tree is a tree representation of the syntacticstructure of the JavaScript code. Ignition, the interpreter, generates bytecode from this syntax tree. TurboFan, the optimizing compiler, eventually takes the bytecode and generates optimized machine code from it.”

Image result for google's v8 engine

Why V8 performs much better is because of its process in this pipe line through the ignition interpreter and the compiler TurboFan.

I would highly recommend taking a look at Franziska Hinkelmann’s blog to get a deeper understanding on what is happening under the hood. https://medium.com/@fhinkel

Additional References:

https://hacks.mozilla.org/2017/02/a-crash-course-in-just-in-time-jit-compilers/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK