Created
February 10, 2011 20:16
-
-
Save sdepold/821243 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var benchmark = function(func, label) { | |
label = label || "benchmark" | |
console.time(label) | |
func.call(null) | |
console.timeEnd(label) | |
} | |
var arr = [] | |
var isEven = function(elem) { | |
return elem % 2 == 0 | |
} | |
for(var i = 0; i < 10000000; i++) { | |
arr.push(i) | |
} | |
Array.prototype.filterCustom = function(callback) { | |
var filtered = [] | |
for(var i = 0; i < this.length; i++) { | |
if(callback(this[i])) filtered.push(this[i]) | |
} | |
return filtered | |
} | |
benchmark(function() { arr.filterCustom(isEven) }, 'custom filter function') | |
benchmark(function() { arr.filter(isEven) }, 'native filter function') |
tested in node and chrome
and what was the result?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the custom filter function is much faster than the native one...