Performance testing and comparison _javascript techniques for 3 methods of JavaScript array

Source: Internet
Author: User
Yesterday to participate in a front-end interview, which has an array to weight, the first thought is the object key value method, the code is as follows
method One: (Simple deposit key value)
Copy Code code as follows:

Array.prototype.distinct1 = function () {
var i=0,tmp={},that=this.slice (0)
this.length=0;
for (; i<that.length;i++) {
if (!) ( That[i] in tmp)) {
This[this.length]=that[i];
Tmp[that[i]]=true;
}
}
return this;
};

The above method is not complex, the idea is simple, but encounter different types can be converted to the same string of the end, such as 1 and "1"; and then use the traditional double loop, the code is as follows
method Two: (double loop)
Copy Code code as follows:

Array.prototype.distinct2 = function () {
var i=0,flag,that=this.slice (0);
this.length=0;
for (; i<that.length;i++) {
var tmp=that[i];
Flag=true;
for (Var j=0;j<this.length;j++) {
if (this[j]===tmp) {flag=false;break}
}
if (flag) this[this.length]=tmp;
}
return this;
};

The above method obtains the desired result, but the two-layer cycle efficiency is low, we'll find a way to get to the top of the first method, and then add a string to hold the type of the array item, and a new type is added to the connection string, and a saved type is found to replace the string of the stored type with NULL, as follows
Method Three: (Deposit key value and type)
Copy Code code as follows:

Array.prototype.distinct4 = function () {
var i=0,tmp={},t2,that=this.slice (0), one;
this.length=0;
for (; i<that.length;i++) {
One=that[i];
T2=typeof one;
if (!) ( One in tmp)) {
This[this.length]=one;
Tmp[one]=t2;
}else if (tmp[one].indexof (T2) ==-1) {
This[this.length]=one;
Tmp[one]+=t2;
}
}
return this;
};

In order to distinguish the different data of various algorithms of the efficiency gap, take several extreme examples to verify, first look at 1-80 all array items are not the same cycle 1000 times, okay, IE6 weak burst

IE9: Chrome:
Firefox:
IE6:


The following is a total of 80 repeat 1000 times of the case, combined with the above data found in addition to ie6-8 other browsers double loop performance is good, and ie6-8 double cycle is 10-20 times slower, sad reminders ah. If your site only supports IE9 above, you can safely use the double loop method, otherwise, the use of the health value method, according to the situation of the data to choose the use of method one or three (figure of method four, only to find too late to change the map, the original method three is using the array of indexof, Because the speed is slow and incompatible is not released.

Ie9:chrome:
FIREFOX:IE6:

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.