V8 has multiple threads for handling the code, these include:
The main thread, to fetch the code, compile it and then execute it
Another thread for optimizing the code while the main thread is getting compiled
Some of them are used for Garbage Handling, allocating the memory and sweeping the unused memory
These threads are used to see how optimizing works
V8 has 2 compilers:
CrankShaft – It uses JIT to create optimized and efficient code. The CrankShaft is also termed as V8's optimizing compiler.
Various optimization Techniques used by V8:
var a=new coordinates(2,3);
var b=new coordinates(4,8);
In the above example, a hidden class is created from the property coordinates, a and b belongs to the same hidden class. V8 will call same hidden class until the time new hidden class created.
Whenever a new property is added to an object a new hidden class created by the V8. It always saves the last created hidden class.
Garbage Collection (GC) Technique: V8 uses the traditional GC approach. It is also called the mark n sweep approach. It uses incremental marking of every possible object and walks the marked part of the heap instead of trying to execute the whole heap. Trying to walk heap parts step by step with the time gaps. The sweep phase will be handled by a separate thread.
Tagged Values: V8 uses 32 bit for both the object and the number. It uses a bit for identifying an object or an integer. For an object ( flag=1) and for an integer ( flag=0). The code will run fast if it does not have various data types like strings, doubles, etc, thus our V8 can only relate to the integers and ignore others.
Array handling: V8 has two methods for handling arrays
a) Fast handling: In this method, it uses linear storage buffer for the arrays which have compact keys.
b) Dictionary Elements: It is more complex than the fast handling technique and is used for the arrays which have sparse arrays. This technique uses a Hashtable.
Hope you liked the article. Share your feedbacks and valuable suggestions in the comments section below.