Skip to content

Instantly share code, notes, and snippets.

@Gozala
Created May 16, 2012 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gozala/2714933 to your computer and use it in GitHub Desktop.
Save Gozala/2714933 to your computer and use it in GitHub Desktop.
Iterators are slover
function time(f) {
var start = Date.now()
f()
return Date.now() - start
}
var array = new function() {
var value = [], i = 0
while (i < 100000) value.push(i++)
return value
}
var iterator = function() {
var i = 0
while (i < 100000) yield i++
}
function increment(x) { return x + 1 }
function isOdd(n) { return n % 2 }
var iteratorTime = time(function() {
for each (let x in iterator()) {
if (isOdd(x)) increment(x)
}
})
var arrayTime = time(function() {
array.filter(isOdd).forEach(increment)
})
iteratorTime - arrayTime
/*
32
*/
@Gozala
Copy link
Author

Gozala commented May 16, 2012

Unfortunately this does not works http://jsperf.com/moz-iterators

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