Skip to content

Instantly share code, notes, and snippets.

@tarzak
Created November 2, 2015 09:28
Show Gist options
  • Save tarzak/4cf5a49de4ce2692cd27 to your computer and use it in GitHub Desktop.
Save tarzak/4cf5a49de4ce2692cd27 to your computer and use it in GitHub Desktop.
var arr = [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]
, i
, sum
;
console.time("while");
i = arr.length;
sum = 0;
while (i--)
sum+=arr[i]
console.timeEnd("while");
console.time("for");
length = arr.length;
i = 0;
sum = 0;
for (; i < length; i += 1)
sum += arr[i];
console.timeEnd("for");
console.time("reduce");
arr.reduce(function(prev,cur) {
return prev += cur;
}, 0);
console.timeEnd("reduce");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment