avatarKevin Duarte

Summary

The provided content discusses the inner workings and advantages of Google's V8 JavaScript engine, which is integral to the performance of JavaScript in web browsers like Chrome and server-side platforms like Node.js.

Abstract

The V8 JavaScript engine, developed by Google, is a key component that enables the execution of JavaScript code in browsers like Chrome and on servers through Node.js. It is a just-in-time (JIT) compiler, which means it combines the benefits of traditional compilers and interpreters to optimize code execution. The V8 engine stands out for its high performance, achieved through its sophisticated compilation pipeline involving an interpreter called Ignition and an optimizing compiler named TurboFan. This pipeline allows the engine to efficiently convert JavaScript code into machine code, with the ability to optimize frequently executed code on-the-fly for better efficiency. The content also lists other JavaScript engines, such as Rhino, SpiderMonkey, and JavaScriptCore, and emphasizes the V8 engine's superiority in performance benchmarks.

Opinions

  • The V8 engine is considered to be "tuned" for high performance, potentially rivaling the speed of languages like C.
  • The author suggests that the combination of the Ignition interpreter and the TurboFan optimizing compiler gives V8 an edge in performance.
  • There is an endorsement for readers to explore Franziska Hinkelmann's blog for a deeper understanding of the V8 engine's internal processes.
  • The content implies that the V8 engine's JIT compilation approach, with its categorization of code into "warm," "hot," or "very hot," is a key factor in its efficient performance.
  • The author indicates that the V8 engine's design allows for both client-side and server-side usage, highlighting its flexibility and versatility.

Basics of understanding Chrome’s V8 Engine

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 syntactic structure 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.”

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/

JavaScript
Chrome
Compilers
Interpreters
Google
Recommended from ReadMedium