Performance Test and comparison of three methods for removing javascript Arrays

Source: Internet
Author: User

I attended a front-end interview yesterday, with an array of removing duplicates. The first thought was the method for storing key values in objects. The Code is as follows:
Method 1: (simple key value storage) Copy codeThe Code is 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, and the idea is simple, but it is enough to convert different types into the same string, such as 1 and "1 "; therefore, the traditional dual loop is used. The Code is as follows:
Method 2: (Dual Loop) Copy codeThe Code is 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 gets the desired result, but the two-layer loop efficiency is relatively low. We can try to get the first method and then add a string to save the type of the array item, if a new type is available, the connection string is added. When searching, a saved type is found to replace the saved type string with null. The Code is as follows:
Method 3: (save key value and type)
Copy codeThe Code is 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 differentiate the efficiency gaps of various algorithms of different data, take several extreme examples to verify that, first of all, we should look at the situation where all array items of 1-80 are different for 1000 cycles, okay, IE6 is weak.

IE9:Chrome:
Firefox:
IE6:

The following is 80 items all repeated loop 1000 times, the above data together found that in addition to the IE6-8 other double loop of the browser performance is good, and the IE6-8 double cycle to slow about 10-20 times, sad reminder. If your website only supports IE9 or above, you can use the double loop method with confidence. Otherwise, you can still use the health check method, select method 1 or method 3 based on the data (Method 4 in the figure shows that it is too late to change the image. The original method 3 uses the Array indexOf, because the speed is slow and incompatible, it is not released)

IE9: Chrome:
Firefox: IE6:

Related Article

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.