Created
February 15, 2017 13:43
-
-
Save b3rew/905f880d0d6270247f8736097e9d3d11 to your computer and use it in GitHub Desktop.
reduce vs for loop
This file contains hidden or 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
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