Test name | Executions per second |
---|---|
filter | 190343.5 Ops/sec |
push | 193901.3 Ops/sec |
var array = [];
for (var idx = 0; idx < 1000; idx++) {
array[idx] = idx;
}
function oddNum(item) {
return item % 2;
}
// Create a new array and push to it
Array.prototype.pushFilter = function(condition) {
// Create a new array and push to it
var result = [];
for(var i = 0, len = this.length; i < len; i++) {
if (condition(this[i], i, this))
result.push(this[i]);
}
return result;
};
var result = array.filter(oddNum);
var result = array.pushFilter(oddNum);