Business- technology - Educational- Politics -Software

Wednesday, May 15, 2019

Understanding V8 engine of Javascript or Node Js for client(Chrome Browser) and server.



Node/Javascript v8 engine
Hello friends , how are you ? In our Node JS part one we learn what is Node Js .  So today we are going to understand node Js V8 engine . V8 engine was developed by google in Germany ,It is developed using c++ ,V8 engine is a open source and mainly made for Chrome browser  at client side but now it has implemented on server . V8 engine was designed to increase performance of Javascript inside web browser .To increase performance of javascript inside browser , V8 engine directly convert javascript into more efficient machine code instead of using any interpreter. It using JIT compiler to compile javascript machine code at the time of execution . Other engines like SpiderMonkey or Rhino (Mozilla)  are also doing the same things but only difference  V8 engine does not produce any bytecode or any intermediate code which makes V8 engine faster . A question may be running in your mind that is it important to know V8 engine ,but my answer will be it will help you to write better and optimised code .JavaScript is a prototype-based language ,so there are no classes and objects ,and classes and objects are created by using a cloning process. JavaScript is also dynamically typed, dynamic means, All variables are dynamic , and even the code is dynamic. You can create new variables at runtime, and the type of variables is determined at runtime. You can create new functions at any time, or replace existing functions. When used in a browser, code is added when more script files are loaded, and you can load more files any time you like. and type informations are not explicit and properties can be added to and deleted from objects on the fly. Accessing types and properties effectively makes a first big challenge for V8. Instead of using a dictionary-like data structure for storing object properties and doing a dynamic lookup to resolve the property location (like most JavaScript engines do), V8 creates hidden classes, at runtime, in order to have an internal representation of the type system and to improve the property access time.V8 can compile to x86, ARM or MIPS instruction set architectures in both their 32- and 64-bit editions; as well, it has been ported to PowerPC and IBM s390 for use in servers.simply V8 convert directly native machine code rather than following traditional technique to convert code into bytecode and than interpret that code to machine code all these things always took some more time which was resolved by v8 engine .v8 engine has one main thread which do the job of fetching the code and compiling it and execute it .There’s also a separate thread for compiling, so that the main thread can keep executing while the former is optimising the code.the concept here is of Hidden class in v8 engine, let's understand hidden class concept of V8 engine.
Hidden class:


function Test(a, b) {
    this.a = a;
    this.b = b;
}
var t1 = new Test(1, 2);
Here V8 is adding a hidden class to keep track and also keep updating this hidden class on any changes into the class properties

here Once the “new Point(1, 2)” invocation happens, V8 will create a hidden class called “Po”.again when next time any changes happen V8 will update this class with another gidden class with name Po2 .
There must be a question coming in your mind that , why we need this hidden class in V8 engine .
There must be a question coming in your mind that , why we need this hidden class in V8 engine . As we have already mention Javascript is a dynamic programming , it means properties type and properties can be changes on run time , it means data stored in memories is not contiguous , so finding fetching something take always longer as compare to Java , because in Java properties are defined before compilation , which means memory allocation is contiguous . And finding from contiguous memory is faster . So V8 engine introduced hidden class concept .On each update in class properties , v8 attaching a updated hidden class , so that at run time it can keep track of properties types .Inline Caching, V8 maintains a cache of the type of objects that were passed as a parameter in recent method calls, and uses that information to make an assumption about the type of object that will be passed as a parameter in the future. If V8 is able to make a good assumption about the type of object that will be passed to a method, it can bypass the process of figuring out how to access the objects properties, and instead use the stored information from previous lookups to the objects hidden class.



No comments:

Post a Comment