Skip to content

Instantly share code, notes, and snippets.

@sdepold
Created February 10, 2011 20:16
Show Gist options
  • Save sdepold/821243 to your computer and use it in GitHub Desktop.
Save sdepold/821243 to your computer and use it in GitHub Desktop.
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')
@sdepold
Copy link
Author

sdepold commented Feb 10, 2011

the custom filter function is much faster than the native one...

@sdepold
Copy link
Author

sdepold commented Feb 10, 2011

tested in node and chrome

@markrambow
Copy link

and what was the result?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment