Home > Web Front-end > JS Tutorial > body text

Javascript updates uniq method of JavaScript array_javascript tips

WBOY
Release: 2016-05-16 19:06:20
Original
1615 people have browsed it

In the last article I wrote, "The uniq method of JavaScript arrays", I found that the problem with the code still exists. For example, if there are undefined elements in the array, it cannot be filtered.

Yesterday I saw Brother Lazy updated the function, and 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                                                                                                                        return false;
}

resultArr.push(this[0]);
for (var i = 1; 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 this[i] = resultArr[i];
}

return returnArr;
}According to what he said: "This solution only changes the original array twice in the whole process, and the efficiency is about 2 orders of magnitude higher than the other two!", I actually measured the efficiency of this function. , indeed (test connection point here).
I also re-wrote and updated my function, it now looks like this:

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

for(var i = 0; i var push = true;
for(var j = i 1; j                                                                                                                                     }

if(push) { tmp.push(this[i])
} }

this.length = tmp.length;
for (var i = 0; i this[i] = tmp[i];
}

return tmp;
} Tested from the same page, the efficiency is still that of the Lazy brothers Slightly faster. After a little thinking, I have some ideas:

My function for nesting can be independent of a function (just like the include function of the Lazy brothers). In the above situation, calling a function will be more efficient than loop judgment.
Special attention should be paid to efficiency issues when performing cyclic read and write operations on arrays when the amount of data is large.
Lazy brothers’ conclusion:
Changing the array is expensive. If possible, try not to change it. Operate on the original array.
If you ultimately need to change the array itself, you can assign the result to the original array for operation. In addition, for the calculation of length
, it seems that the efficiency is not affected. Brother Lazy's resultArr array can save the same value according to his way of writing it. I like it here (although my function can be implemented with a little modification). Interested friends can go to Lazy’s page to have a look.

Finally, I recommend reading the uniq method of JavaScript arrays by Wang Yuantao brothers. Thank you very much.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template