forEach vs for vs for..in vs while

JavaScript performance comparison

Revision 10 of this test case created by

Preparation code

<script>
var j, array = []
for(j=1; j<= 5000; j++){
array[j]= j;
}
var _foreach=function(array,callback){
var len = array.length;
for(var i=1; i< len; i++) callback(array[i],i);
};
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
for
var len = array.length, sum = 0;
for(var i=0; i< len; i++){
sum += array[i];
}
pending…
while222
var sum = 0, num = 0, i= 0
while( num = array[ ++i ] ) {
sum += num;
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments