Uniq Method for updating Javascript arrays using JavaScript

Source: Internet
Author: User
Tags javascript array

I wrote a JavaScript Array uniq method last time and found that the code problem still exists. For example, an undefined element in an array cannot be filtered.

Yesterday I saw the Lazy brother update the function again. Now he writes it like this:

Array. prototype. uniq = function (){
Var resultArr = [],
ReturnArr = [],
OrigLen = this. length,
ResultLen;

Function include (arr, value ){
For (var I = 0, n = arr. length; I <n; ++ I ){
If (arr [I] === value ){
Return true;
}
}

Return false;
}

ResultArr. push (this [0]);
For (var I = 1; I <origLen; ++ I ){
If (include (resultArr, this [I]) {
ReturnArr. push (this [I]);
} Else {
ResultArr. push (this [I]);
}
}

ResultLen = resultArr. length;
This. length = resultLen;
For (var I = 0; I <resultLen; ++ I ){
This [I] = resultArr [I];
}

Return returnArr;
} According to his statement: "This solution only changes the original array twice in the whole process, and the efficiency is about two orders of magnitude higher than the other two !", I tested the efficiency of this function (test the connection point here ).

I wrote and updated my functions again, and now it looks like this:

Array. prototype. uniq = function (){
Var tmp = new Array;
Var length = this. length;

For (var I = 0; I <length; I ++ ){
Var push = true;
For (var j = I + 1; j <length; j ++ ){
If (this [j] === this [I]) {
Push = false;
Break;
}
}

If (push ){
Tmp. push (this [I])
}
}

This. length = tmp. length;
For (var I = 0; I <tmp. length; I ++ ){
This [I] = tmp [I];
}

Return tmp;
} The test result from the same page makes Lazy brothers more efficient. After a bit of thinking, I have a little bit of my experience:

My function for nesting can be independent by a function (just like the include function of the Lazy brother ). In the above cases, calling a function is more efficient than loop judgment.
When there is a large amount of data, the array's cyclic read/write operations should pay special attention to efficiency issues.
Lazy's conclusion:

The overhead of changing the array is huge. If possible, try to operate without changing the original array.
If you need to change the array itself, you can assign the result to the original array for operation. In addition, for length
It seems that the efficiency is not affected. The resultArr array of the Lazy brother can save the same value according to his writing method. Here I like one (although my function can be implemented after a small modification ). If you are interested, you can go to Lazy's page to see it.

Finally, we recommend that you read the uniq method of the JavaScript array of Brother Wang yuantao. Thank you very much.

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.