Browser dictionary: V8

Source: Internet
Author: User
Tags access properties

V8: Chrome's JavaScript Engine, developed with C ++, based on the ECMA-262 3rd standard. Anyone can participate as an open-source project. Project address in Google Code: http://code.google.com/p/v8/

Chrome V8 Design

Since the end of the 19th century, Netscape has integrated JavaScript, making it easier for web developers to access HTML page elements such as forms, frames, and images. JavaScript is becoming increasingly popular. It is used to customize controls and increase animation effects. After 1890s, a large number of scripts were generated to switch images to respond to user-generated mouse events.

Recently, with the emergence of AJAX, JavaScript has become the central technology for implementing web-based applications such as Gmail. JavaScript programs grow from several simple lines to several hundred KB of source code. However, JavaScript is a very effective technology designed to implement web applications. Performance has become a constraint for developing web-based JavaScript applications.

V8 is a brand new JavaScript engine designed to quickly execute a large number of JavaScript script applications. In several benchmark tests, V8 has many times the performance of JScriptIE engines), SpiderMonkeyFirefox, and JavaScriptCoresafari. If your web application is restricted by the speed of JavaScript Execution, replacing your current JavaScript Engine with V8 will likely improve the performance of your application. The performance improvement depends on the amount of JavaScript and the characteristics of JavaScript. For example, if a function tends to be executed at one time in your application, the performance will be greatly improved compared to executing many different functions at one time. After reading this document, you will be more aware of the causes for performance improvement.

Three key aspects of V8 performance:

◆ Quick attribute access

◆ Dynamically generate machine code

◆ Efficient garbage collection

Quick attribute access

JavaScript is a dynamic programming language: the attributes of an object can be added or deleted. This means that the attributes of an object may change. Most JavaScript Engines use dictionary-like data structures to store the attributes of objects. Access to each attribute requires a dynamic search to locate the location of the attribute in the memory. This typical access attribute method is much slower than accessing instantiated variables in Java and Smalltalk. In these languages, instantiated variables are located through a fixed offset defined by the compiler based on the fixed layout of objects defined by the object type. Loading or storage access is very simple. Generally, you only need a simple command.

To reduce the time required to access JavaScript properties, V8 does not use dynamic search for access properties. Instead, V8 dynamically creates hidden classes in the background. This idea is not the latest-it is the prototype-based programming language that uses similar features to map something.) See An Efficient Implementation of Self, a Dynamiclly-Typed Object-Oriented Language Based on Prototypes ). In V8, when a new property is added, the object changes its hidden class.

To clear this point, imagine the next simple JavaScript function:

function Point(x, y) {  this.x = x;  this.y = y; }

When new Point (x, y) is executed, a new Point object is created. When V8 is first created, V8 creates an initial hidden class Point, which is called C0 in this example. If the object does not have any attribute at the beginning, an empty initial class is defined. The hidden class of the Point object is C0.

Execute the first statement (this. x = x;) in the Point object to create a new attribute x. In this case, V8:

Execute the second statement (this. y = y;) of Point, and create a new attribute y in the Point object. In this case, V8:

Adding a property at any time does not seem very efficient by creating a hidden class. However, due to the transition of classes, hidden classes can be reused, and the actual efficiency is high. The second time you create a new Point, you do not need to create a new hidden class. On the contrary, the new Point object shares the type of the first Point object. For example, if you create another Point object:

Although JavaScript is more dynamic than the common object-oriented language, the runtime behavior of common JavaScript programs using the above method will lead to a high degree of structural contribution. Here we list two advantages of using hidden classes: attribute access does not require dictionary searches, and V8 can use object-oriented optimization and inline caching. For more inline caching, see Efficent Implementation of the Smalltalk-80 System.

Dynamically generate machine commands

During the first execution, V8 directly compiled the JavaScript source code into a machine code. The bytecode of the intermediate process does not exist, and there is no interpreter. The access attribute is implemented by processing the inline cache code, which can be converted to other machine commands like V8 during execution.

When you access the attributes of a given object for the first time, V8 generates the current hidden class of the object. V8 uses a hidden class to internally generate inline Cache Information and optimizes attribute access by predicting whether the class will be used for all future objects in the same code section. If V8 is successfully predicted, the attribute value will be read or written in a simple operation. If the prediction is incorrect, V8 deletes the optimized code.

For example, the JavaScript code accesses the attribute x of the Point Object:

point.x

In V8, the machine code used to access x is:

# ebx = the point objectcmp [ebx,

If the hidden class of the object does not match the hidden class of the cache, the system will jump to the V8 runtime to process the missing embedded cache and generate embedded cache code. The common situation is matching, returns the value of attribute x.

When many objects share the same hidden class, these objects benefit from the same as most static languages. The hidden class access attributes and the embedded cache are combined with the machine code generation optimization. For objects of the same type, they are frequently created and accessed in a similar way, this will greatly increase the speed of executing most JavaScript code.

Efficient garbage collection

V8 recycles the memory of objects that are no longer needed in the process. This process is called garbage collection. To ensure fast object allocation and free-of-memory fragments, V8 uses the stop-the-world, generational, and precise garbage collector. This means V8:

In V8, the object heap is divided into two parts: the new space of the newly created object and the old object still in use during garbage collection. If an object is recycled by the garbage collector, V8 updates all pointers to this object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.