Skip to content

Instantly share code, notes, and snippets.

@b3rew
Created February 15, 2017 13:43
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 b3rew/905f880d0d6270247f8736097e9d3d11 to your computer and use it in GitHub Desktop.
Save b3rew/905f880d0d6270247f8736097e9d3d11 to your computer and use it in GitHub Desktop.
reduce vs for loop
var top = 10000000;
var glass = [];
var t = 0;
var sum = 0;
for(var i = 0;i<top;i++){
t += i;
glass[glass.length] = {amount: t}
}
console.time("for time");
var l = glass.length;
for(var i = 0;i<l;i++){
sum += glass[i].amount;
}
console.timeEnd("for time");
console.info("for total ", sum)
console.log("-------------------------------")
sum = 0;
console.time("reduce time");
sum = glass.reduce(function (last, d) {
return parseInt(d.amount + last);
});
console.timeEnd("reduce time");
console.info("reduce total ", sum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment