forEach vs for

JavaScript performance comparison

Test case created by Andreas Goebel

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = [5,4,3,2,1];
   
    function DoStuff( n ) {
        n = n;
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
for
for( var i = 0; i < arr.length; i++ ) {
    DoStuff( arr[i] );
}
pending…
forEach
arr.forEach(function( elem ) {
    DoStuff( elem );
});
pending…

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

Compare results of other browsers

0 comments

Add a comment