ES5 forEach vs For vs While (v7)

Revision 7 of this benchmark created 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

Revisions

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