Created
October 21, 2010 05:35
-
-
Save uhop/638003 to your computer and use it in GitHub Desktop.
Iterating over array
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
// Benchmarking summation of array | |
var a = []; | |
for(var i = 0; i < 100; ++i) a.push(Math.random()); | |
this.group( | |
"Iterations", | |
function classic(){ | |
for(var sum = 0, i = 0, l = a.length; i < l; ++i){ sum += a[i]; } | |
}, | |
function short(){ | |
for(var sum = 0, i = 0, l = a.length; i < l; sum += a[i++]); | |
}, | |
function forEach(){ var sum = 0; a.forEach(function(x){ sum += x; }); }, | |
function reduce(){ var sum = a.reduce(function(a, b){ return a + b; }, 0); }, | |
function reduceRight(){ var sum = a.reduceRight(function(a, b){ return a + b; }, 0); } | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Benchmark it with http://www.perfjs.com/