Created
January 8, 2015 00:25
-
-
Save lykkin/9c5e50424d5156e6cedc to your computer and use it in GitHub Desktop.
slooooooow
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
asyncMap = function(fn, list, callback){ | |
var counter = 0; | |
var result = list.slice(); | |
list.forEach(function(elem, index){ | |
setTimeout(function(){ | |
if(++counter === result.length){ | |
callback(result); | |
} | |
result[index] = fn(elem); | |
}, | |
0) | |
}); | |
} | |
syncMap = function(fn, list, callback){ | |
var result = []; | |
list.forEach(function(elem){ | |
result.push(fn(elem)); | |
}); | |
callback(result); | |
} | |
time = function(f){ | |
var res = []; | |
for(var i = 0; i < 1000000; i++){ | |
res.push(i); | |
} | |
asyncstart = new Date(); | |
asyncMap(function(a){return f(a)}, res, function(){console.log('asyncMap ran in ' + (new Date() - asyncstart) + ' milliseconds')}) | |
syncstart = new Date(); | |
syncMap(function(a){return f(a)}, res, function(){console.log('syncMap ran in ' + (new Date() - syncstart) + ' milliseconds')}) | |
} | |
time(function(a){return a + 1}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment