Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How V8 JavaScript Engine Works

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 6.41k
    Comment on it

    V8

    V8 is a Javascript Engine which is used to increase the efficiency of javascript code during the web browsing. It is an open source and can be used for the clients as well as the server side. Google chrome and node.js are the main examples of V8 on the client side and server side respectively. It uses JIT (Just In Time ) compiler to convert the source code directly into the native machine language instead of converting it into an intermediate code. It uses memory allocation and garbage collection techniques.

    Image Source - Google

     

    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:

    1. Full-codegen It produces machine code for any javascript source code, it generates the code fast and operates much faster than the other compilers.

    2. CrankShaftIt uses JIT to create optimized and efficient code. The CrankShaft is also termed as V8's optimizing compiler.

     

    Various optimization Techniques used by V8:

     

     

    Hidden Classes – As Javascript is a prototype-based language that does not use class but relies upon cloning for the defining objects. During the runtime, it uses hidden class for its own understanding to access the property.

    For example:

    function coordinates(x,y)
    {
    this.x=x;
    this.y=y;
    }
    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.

     

     How V8 JavaScript Engine Works

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: