foreach vs for (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script>
  var array = [];
  var o;
  for (var i = 0; i < 1000; i++) {
   array[i] = i;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
for
var len = array.length;
o = 0;
for (var j = 0; j < len; j++) {
 o += array[j];
}
ready
for (without length opt.)
o = 0;
for (var j = 0; j < array.length; j++) {
 o += array[j];
}
ready
foreach
o = 0;
array.forEach(function(val) {
 o += val;
});
ready

Revisions

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