Array.forEach vs native for

JavaScript performance comparison

Revision 3 of this test case created by Thomas Upton

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var values = Array(100);
    for (var i=0; i < 100; i++) {
      values[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
Native for
var sum = 0;
for (var i = 0, ii = values.length; i < ii; i++) {
    sum += values[i];
}
pending…
Array.forEach
var sum = 0;
values.forEach(function(value) {
  sum += value;
});
pending…
_.forEach
var sum = 0;
_.forEach(values, function(value) {
  sum += value;
});
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment