Skip to content

Instantly share code, notes, and snippets.

@lykkin
Created January 8, 2015 00:25
Show Gist options
  • Save lykkin/9c5e50424d5156e6cedc to your computer and use it in GitHub Desktop.
Save lykkin/9c5e50424d5156e6cedc to your computer and use it in GitHub Desktop.
slooooooow
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