for vs forEach (v427)

Revision 427 of this benchmark created on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

var i, values = [],
        sum = 0;
    for (i = 0; i < 10000; i++) {
     values[i] = i;
    }
    
    function add(val) {
     sum += val;
    }
  
  function addIdxVal(idx, val) {
     sum += val;
  }
  
  function addIdx(idx){
  sum += values[idx];
  }

Test runner

Ready to run.

Testing in
TestOps/sec
forEach
values.forEach(add);
ready
for loop, simple
for (var i = 0; i < values.length; i++) {
  add(values[i]);
}
ready
for loop, cached length
var len = values.length;
for (var i = 0; i < len; i++) {
  add(values[i]);
}
ready
for...of
for(var val of values){
   add(val);
}
ready
angular.forEach
angular.forEach(values, add);
ready
$.each - both
$.each(values, addIdxVal);
ready
$.each - idx
$.each(values, addIdx);
ready

Revisions

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