Optimized compact method by using a counter in place of Array.prototype.push()

This commit is contained in:
robertmesserle
2013-12-06 09:32:25 -08:00
committed by John-David Dalton
parent 3633d3cd73
commit 9a1de81fc5

View File

@@ -4501,12 +4501,13 @@
function compact(array) { function compact(array) {
var index = -1, var index = -1,
length = array ? array.length : 0, length = array ? array.length : 0,
result = []; result = [],
cur = 0,
value;
while (++index < length) { while (++index < length) {
var value = array[index]; if (value = array[index]) {
if (value) { result[cur++] = value;
result.push(value);
} }
} }
return result; return result;