Array.forEach vs native for (v3)

Revision 3 of this benchmark created by Thomas Upton on


Preparation HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js"></script>

Setup

var values = Array(100);
    for (var i=0; i < 100; i++) {
      values[i] = i;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Native for
var sum = 0;
for (var i = 0, ii = values.length; i < ii; i++) {
    sum += values[i];
}
ready
Array.forEach
var sum = 0;
values.forEach(function(value) {
  sum += value;
});
ready
_.forEach
var sum = 0;
_.forEach(values, function(value) {
  sum += value;
});
ready

Revisions

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