for vs forEach (v87)

Revision 87 of this benchmark created by BrunoLM on


Preparation HTML

<script>
  var i, values = [],
      sum = 0;
  for (i = 0; i < 10000; i++) {
   values[i] = i;
  }
  
  function add(val, index) {
   sum += val + index;
  }

   Array.prototype.CustomForEach = function(fn) {
       for (var i = 0, n = this.length; i < n; ++i)
           fn(this[i], i);
   };

   Array.prototype.CustomForEachCacheRef = function(fn) {
       var a = this;
       for (var i = 0, n = a.length; i < n; ++i)
           fn(a[i], i);
   };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
forEach
values.forEach(add);
ready
for
for (var i = 0, n = values.length; i < n; ++i) {
  add(values[i], i);
}
ready
CustomForEach
values.CustomForEach(add);
ready
CustomForEachCacheRef
values.CustomForEachCacheRef(add);
ready

Revisions

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