for vs forEach (v317)

Revision 317 of this benchmark created on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>

Setup

<script>

        Benchmark.prototype.setup = function() {

            var i,
      value,
      length,
      values = [],
      sum = 0,
      context = values;
  
  
  for (i = 0; i < 10000; i++) {
      values[i] = Math.random();
  }
  
  function add(val) {
      sum += val;
  }
        
  };

Teardown



            i = 0;
  value = 0;
  length = 0;
  values = [];
  sum = 0;
        
  

Test runner

Ready to run.

Testing in
TestOps/sec
forEach
values.forEach(add);
ready
for loop, simple
for (i = 0; i < values.length; i++) {
    sum += values[i];
}
ready
for loop, cached length, callback
length = values.length;
for (i = 0; i < length; i++) {
    add(values[i], i, values);
}
ready
angular.forEach
angular.forEach(values, function(key, value) {
    sum += value;
});
ready
_.forEach
_.forEach(values, function(key, value) {
    sum += value;
});
ready

Revisions

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