for vs for vs forEach

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = [];
   
    for (var i = 0, j = 100; i < j; i++ ) {
      arr[i] = i;
    }
};
</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 getting length each iteration
var res;
for (var i = 0; i < arr.length; i++ ) {
  res = arr[i];
}
pending…
for getting length on start
var res;
for (var i = 0, j = arr.length; i < j; i++ ) {
  res = arr[i];
}
pending…
forEach
var res;
arr.forEach(function(el) {
  res = el;
});
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