for vs while loops, positive and negative (v4)

Revision 4 of this benchmark created on


Setup

const array = Array.from({ length: 10000000 }, (_, i) => i);

Test runner

Ready to run.

Testing in
TestOps/sec
for loop, negative
let sumFor = 0;
for (let i = array.length; i --;) {
  sumFor += array[i];
}
ready
while loop
let i = 0;
let len = array.length
let sumWhile = 0;
while (i < len) {
  sumWhile += array[i++];
}
ready
for loop, positive
let sumFor = 0;
let len = array.length
for (let i = 0; i < len; i++) {
  sumFor += array[i];
}
ready
For of
let sumForOf = 0;
for (const item of array) {
  sumForOf += item; // Process item
}
ready
Inverted while
let l = array.length;
let sumWhile = 0;
while (l) {
  sumWhile += array[l--];
}
ready

Revisions

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