ES5 forEach vs For vs While (v9)

Revision 9 of this benchmark created by Erwan Legrand on


Preparation HTML

<script>
  // it's truly common and BAD practice to create functions runtime
  // per each forEach, map, some, every, filter or other iteractions
  // create functions you need and reuse them, valid for DOM events too
 
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
forEach
arr.forEach(function(el){el.a++;});
ready
for
for(var i=0;i<arr.length;i++){arr[i].a++;}
ready
for 2
var lgth=arr.length;for(var i=0;i<lgth;i++){arr[i].a++;}
ready
while
var i=arr.length;while(i--){arr[i].a++;}
ready
for + callback
for(var i=0;i<arr.length;i++){c(arr[i]);}
ready
while + callback
var i=arr.length;while(i--){c(arr[i]);}
ready
reversed while
var i=arr.length;while(--i){arr[i].a++;}
ready
reversed while + callback
var i=arr.length;while(--i){c(arr[i]);}
ready
custom foreach
arr.forE(anotherPrep);
ready
forEach + callback
arr.forEach(avoidFunctionCreationPerEachIteraction);
ready
for 3
for(var i = 0, a = arr, l = arr.length; i < l; i++) {
        a[i].a++;
}
ready
for + callback 2
for(var i = 0,
        a = arr,
        l = arr.length,
        f = function(e){e.a++;};
     i < l;
     i++) {
        f(a[i]);
}
 
ready

Revisions

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