About chrome sort () Optimization

Source: Internet
Author: User

When I read the sizzle code today, there is such a section:

// Here we check if the JavaScript engine is using some sort of
// optimization where it does not always call our comparision
// function. If that is the case, discard the hasDuplicate value.
// Thus far that includes Google Chrome.
[0, 0].sort(function() {
baseHasDuplicate = false;
return 0;
});

Then google found this article: "Chrome V8 engine optimization for sort", which is simply copied:

VaR a = 0, B = 0;
[0, 0]. Sort (function (){
A = 1;
Return 0;
});
[0, 1]. Sort (function (){
B = 1;
Return 0;
});
Alert (A = B); // true or false?
The above Code except chrome outputs false, all other browsers are true.

The reason is that Chrome has optimized the array sort method:

Function sort (comparefn ){
VaR custom_compare = (typeof (comparefn) = 'function ');
Function compare (x, y ){
If (x = y) return 0;
If (custom_compare ){
Return comparefn. Call (null, x, y );
}
...
}

 

However, after testing, we found that this was not true. In Chrome 16.0.912.77 M, No optimization was executed. Alert came out with true, which is the same as ff and other browsers.

Therefore, this should not exist in all chrome versions, so it cannot be final.

 

The test result does not cover all chrome versions. Please note the error.

 

For more information, see http://www.cnblogs.com/xesam /]
Address: http://www.cnblogs.com/xesam/archive/2012/02/03/2336921.html

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.