JavaScript update JavaScript array Uniq method _javascript tips

Source: Internet
Author: User
Tags arrays javascript array
The last written "Uniq method for JavaScript arrays" found that the problem with the code was still there. For example, if there are undefined elements in the array can not be filtered.

Yesterday I saw the Lazy brother renew the function, and now he writes 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 saying: "This solution in the whole process of the original array of changes only two times, the efficiency of the other two higher than 2 orders of magnitude!" "I have measured the efficiency of this function, indeed (test connection point here).

I also rewritten and updated my function, 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;
Test results from the same page, efficiency or Lazy brother a little faster. After a bit of thinking, I have a little bit of my experience:

My function for nesting can be independent of a function (just like the Lazy brother's include function). In the above case, the calling function is more efficient than the cyclic judgment.
The circular read-write operation of the array should pay attention to the efficiency problem in the case of large amount of data
Lazy Brothers ' conclusion:

The changes in the array are costly and, if possible, manipulated without altering the existing arrays.
If you eventually need to change the array itself, you can assign the result to an existing array to operate. In addition, for length
Calculation, it seems that efficiency is not affected by it. The Resultarr array of the Lazy brothers the same value can be saved in this way, and here's a nice one (although my function can be implemented with a little bit of modification). Interested friends can go to Lazy's page to see.

Finally, it is highly appreciated to read the Uniq method of the Wang Yuantao sibling's JavaScript array.
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.