Why does the convertToFastObject function do this fast?

I tried the Dart SDK after release 1.0 and wrote a simple hello-world program in Dart. Then, using the SDK tool, I generated a JavaScript file: helloworld.dart.js I went through the js output code, I saw that there is a function called convertToFastObject. Definition:

function convertToFastObject(properties) { function MyClass() {}; MyClass.prototype = properties; new MyClass(); return properties; } 

The usage code is similar:

 A = convertToFastObject(A); B = convertToFastObject(B); 

I know that this code is for different types of browsers, not just Chromium / Chrome. I can’t understand why a function can make an object faster?

+7
performance javascript dart
source share
2 answers

This is speed optimization for the Google V8 engine.

Of course, this piece of code looks rather strange: it assigns properties as a prototype of the MyClass constructor, then uses the constructor to build the instance with new MyClass() , and then returns properties . This is strange because 1) properties never change, and 2) a function never uses MyClass or an instance again.

Whenever you see strange behavior like this, you can be sure that it is speed optimization. In this case, the speed is achieved through the use of V8 "hidden class" optimization . From a closely related section of Darth's source :

 // Use the newly created object as prototype. In Chrome, // this creates a hidden class for the object and makes // sure it is fast to access. 

In the V8 engine, the constructed object receives a "hidden" C ++ class to represent its set of properties. By creating an object whose prototype is the properties object, the values ​​of the properties properties become part of the hidden class of the new C ++ instance, which improves the speed of access to properties.

I believe that all objects in V8 have hidden classes by default, so the need for this technique is not immediately obvious. However, an object can lose its hidden class (and introduce "slow mode" or "dictionary mode"), demonstrating that this does not contribute to optimization. When a delete object is one of its properties or adds too many properties that are not related to the properties of any other objects, V8 assumes that a shared hidden class is not valuable because the object does not have another similar object to share its hidden class from. This convertToFastObject function can re-set the "slow mode" object directly to the hidden class, using it as a prototype of the newly created instance.

A related hidden class question arising from another Dart optimization: What kind of generated code is this (which is supposed to) do?

+10
source share

If the data is stored in a script, it directly contributes to the amount of time it takes to execute. In general, there are four places from which you can access data in a script:
-Literary value
-variable
-Application of subject
-An object

Reading data always leads to execution cost, and this cost depends on which of the four locations the data is stored in. If you create a property using "Object.Prototype.", The scope here is "Object.Prototype", which is smaller than the "Object" object. which additionally hold local vars and stuff without the enumerations. That's why creating prototypes using Prototype has faster access! Read these 2 articles to better understand: 1- http://oreilly.com/server-administration/excerpts/even-faster-websites/writing-efficient-javascript.html 2- http://www.packtpub.com/ article / using-prototype-property-in-javascript

-one
source share

All Articles