for vs forEach (v264)

Revision 264 of this benchmark created by Jason Mayes on


Description

Is it faster to use the native forEach or just loop with for?

Preparation HTML

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

var x = 0;
  
</script>

Setup

x = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
forEach
values.forEach(function(value) {x+=value;});
ready
for loop, simple
for (i = 0; i < values.length; i++) {
  x+= values[i];
}
ready
for loop, cached length
var len = values.length;
for (i = 0; i < len; i++) {
  x+= values[i];
}
ready
for loop, reverse
for (i = values.length - 1; i >= 0; i--) {
  x+= values[i];
}
ready

Revisions

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