A few days back, I started learning and digging more into the JavaScript V8 Engine. I started learning about how it works and how the compilation of code begins. In this guide, I’ve written a complete guide on JavaScript V8 Engine, make sure you read it completely.
You can call a JavaScript Engine a program or an interpreter. A JavaScript engine basically executes or run the code of JavaScript. A JavaScript V8 Engine is an open source project started by Google and written in C++ in Germany. JavaScript V8 Engine is a similar project like Rhino, Spider Monkey, KJS etc.
How JavaScript V8 Engine Works?
V8 Engine was first developed on the basis of increasing the performance of execution of JavaScript code inside our Web browsers. It translates the JavaScript code into the efficient machine code to obtain speed without using an interpreter. It uses a JIT (Just in Time) compiler to compile the JavaScript code in the Machine code. It does not produce any sort of intermediate code or bytecode in the middle stage.
JavaScript V8 Engine uses two types of compilers: A full-codegen which is a very fast and a simple compiler and produces very simple but slow machine code. The other one is Crankshaft that produces a more complex code but highly optimized.
There are many other internal threads also: The main thread does a simple job like fetching, compiling and then executing the code. There is a separate thread for the compilation of the code. The other few threads are for the garbage collection.
Hidden Classes
As you know JavaScript is dynamic and proto-type based language, hence, there are no objects and classes. But, at the runtime, JavaScript V8 Engine build the hidden classes to access the type system and hence, improving the access time. It was a great challenge to access and get the type information from a dynamic language.
Let us see this in a more explanatory way. If there are one function and two objects, there will be more optimization. How?
Well, as you know V8 produces a new hidden class, that hidden classes would be sufficient for these two objects if they have the same layouts.
Let say if another parameter has included with one of the above mentioned two objects then what should V8 do?
Well, in that case, V8 will produce another hidden class. V8 keeps on producing new hidden classes whenever a new parameter or new construction function will be declaring a property.
Code Optimization
As we know that, V8 creates a new hidden class everytime a new property has added. Hence, hidden classes building should be kept to a minimum scale. Always initialize the members in the same order to avoid the creation of a different tree of hidden class. Make sure you are not adding properties after the creation of an object. Try to use the monomorphic operation because they use the same hidden class again and again.
How JavaScript V8 Engine Compiles the Code?
As we know, JavaScript V8 Engine uses the two compilers for compiling the code. One is the Full Compiler and the other one is the Optimizing Compiler.
The work of the Full Compiler is to generate the code quickly, it generates a good code but not the great one. It uses the IC (Inline Caches) strategy to get the knowledge about the types of the functions while running the programs. This compiler gets your code a 20-25 times speed improvement.
The other compiler which is the Optimizing Compiler that produces a great and more efficient code in JavaScript language. It takes the type from the IC and set the decisions for making the code better.
I hope you like this article on ‘JavaScript V8 Engine’. We also share the latest technology trends, make sure you check them. Follow us on Social Media for the latest updates.