Working of JavaScript’s Chrome V8 Engine

Narendra Kumar
jsblend
Published in
4 min readSep 14, 2021

--

What is V8 Engine?

Well, V8 Engine is opensource JavaScript engine written in C++ which is self-sufficient to run both client-side JavaScript and server-side JavaScript (through NodeJS ecosystem) to enhance user experience over the web & it can be easily embedded into any C++ application. V8’s continuously improving and evolving to speed up the Node JS Ecosystem & the Web. The V8 is based on the JIT (Just in Time) compilation pipeline.

Just in Time Compilation made easy (JIT)

Its meaning is quite self-explanatory since according to JIT the code is compiled just-in-time as we need to run the code. This means looping or iterating the process of compiling a part of code and then running it. So, the process of running the code and compiling is being alternated multiple times. So its advantage is that it takes its flavor from both i.e. a compiler & an interpreter and uses both for small portions of code.

Now the game changer is an added Profiler (collects data from runtime) that sees upon similar code being executed numerous times in a program, that seems similar to an iterating method & tags them as “very hot”, “hot” or just “warm” depending upon the number of times that similar piece of code is being executed. If a certain piece of code is “very hot” then it will be passed on to the optimizing compiler which optimizes the code based on the run time info from the profiler.

Although initially JIT compiler takes time to compile these methods or functions but taking into account the bigger picture (entire code), with time the code will run much more efficiently.

V8 Engine Pipeline

V8 Pipeline Basic Model

The newest V8 Engine compiler pipeline came into action since version 5.9 launched in 2017.

=> Source code is being parsed by the parser to generate the Abstract Syntax Tree.

=> Abstract Syntax Tree is taken as an input by the IGNITION (Interpreter) & gives Byte code as the Output

=> As Byte code is being run, runtime information is being accumulated in the Profiler & then the code is Recompiled

=> So if there are some hot functions present (i.e. functions being run all the time), then it forwards these functions along with some runtime information to the TURBOFAN (Optimizing Compiler)

=> Turbofan compiles the functions to even faster machine code

=> Turbofan is based on speculative optimization which is a form of adaptive optimization since these speculations are being made from runtime information to adapt to better and faster code.

=>If the speculations fail, then it reverts back to comparatively slower byte code.

Insight to the V8 Engine Pipeline

=>So basically it compiles the code and then runs it and along with that it stores some info in the profiler & speculates with this data regarding what’s going to happen in future & that stored runtime information plays a huge role in how it compiles the code after that.

=> Also we don’t have to worry about memory management since V8 has a garbage collector.

On runtime V8 engine is mainly managing the heap memory allocation & single threaded cell stack. Call stack maintains a list of functions to execute in the order in which they were called. Every nested function will be pushed one after the another into the stack & callbacks will be called back to the Call stack in the end.

V8 Engine is single threaded execution engine (i.e. its coded to run exactly one thread per JavaScript execution context), however V8 Engine internally has numerous threads in it :

1. MAIN thread : Its job is pretty much justifies by its name i.e. to fetch the code, compile it & then execute the code

2. Another Thread for Compiling : It helps the main thread by optimizing the code so that the main thread can keep executing its function smoothly

3. Profiling Thread : It tells us in runtime that on which method it is spending a lot of time (hot functions)

4. Threads for Garbage Collection : A few or some threads for garbage collection sweeps

Conclusion

In this article, I explained the working process of JavaScript's Chrome V8 Engine with all the execution steps involves. V8 engine is the backbone of Google Chrome and other Chromium-based web browsers and also distinct from other JS engine as it directly converts script into machine code without producing intermediate code.

--

--

Narendra Kumar
jsblend

Technical Lead | Senior Software Engineer | Full Stack Developer | Technology Enthusiast