Created
May 16, 2012 23:40
-
-
Save Gozala/2714933 to your computer and use it in GitHub Desktop.
Iterators are slover
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
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 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately this does not works http://jsperf.com/moz-iterators