For vs While (v20)

Revision 20 of this benchmark created by SourceR on


Setup

var values = [],
        sum = 0,
        i;
    
    /* I think 1000 is a more realistic value than 10000.
    */
    for (i = 0; i < 1000; i++) {
      values[i] = i+1;
    }
    
    
    /* if the sum is bigger than 1000 it will be set to 0
    ** ´cause the value should not be bigger on on the beginning
    ** of a test as at the beginning of another test before.
    */
    
    function add(val) {
      sum == 1000 ? (sum = 0) : (sum += val);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
ForEach
values.forEach(add);
ready
for loop, simple
for (i = 0; i < values.length; i++) {
  add(values[i]);
}
ready
for loop, simple (with var)
for (var i = 0; i < values.length; i++) {
  add(values[i]);
}
ready
for loop, cached length
var len = values.length;
for (i = 0; i < len; i++) {
  add(values[i]);
}
ready
for loop, reverse
for (i = values.length - 1; i >= 0; i--) {
  add(values[i]);
}
ready
for loop, reverse (not equal, with var)
for (var i = values.length - 1; i != 0; i--) {
  add(values[i]);
}
ready
for loop, cached length, no callback
var len = values.length;
for (i = 0; i < len; i++) {
  sum += values[i];
}
ready
for loop, cached length, no callback (one time „var”, [using var], not equal [len-1] instead of less
var len = values.length - 1,
    i = 0;
for (; i != len;) {
  sum += values[i++];
}
ready
for loop, cached length, using function.call and supplying index and original array
var len = values.length;
for (i = 0; i < len; i++) {
  add.call(values, values[i], i, values);
}
ready
While, reversed + simple
var i = values.length;
while (--i >= 0) {
  sum += values[i];
}
ready
While, reversed + simple, leftside --, not linked
var i = values.length + 0;
while (i-- != -1) {
  sum += values[i];
}
ready
While, reversed + callback
var i = values.length;
while (--i >= 0) {
  add.call(values, values[i], i, values);
}
ready
While, reversed + callback, -- right side
var i = values.length;
while (i-- >= 0) {
  add.call(values, values[i], i, values);
}
ready
While
var l = values.length;
var i = 0;
while (i++ <= l) {
  sum += values[i];
}
ready
While, one-time var, not equal
var l = values.length - 1,
    i = 0;

while (i++ != l) {
  sum += values[i];
}
ready
For - prefix decrement, gt0
for (var i = values.length; i >= 0; --i) {
  sum += values[i];
}
ready
For - prefix decrement, not equal
for (var i = values.length - 1; i != 0; --i) {
  sum += values[i];
}
ready
For - prefix decrement, gt
for (var i = values.length; --i >= 0;) {
  sum += values[i];
}
ready
For - prefix uq -1
for (var i = values.length; --i != -1;) {
  sum += values[i];
}
ready
For - gt -1
for (var i = values.length; i > -1; --i) {
  sum += values[i];
}
ready
For with assignment
for (var i = 0; match = values[i]; ++i) {
  add(match);
}
ready
for post-decrement statement
for (var i = values.length; i--;) {
  sum += values[i];
}
ready
for pre-decrement count
for (var i = values.length; i; --i) {
  sum += values[i];
}
ready
for pre-decrement statement, --i!==-1
for (var i = values.length; --i !== -1;) {
  sum += values[i];
}
ready
While, body pre-decrement
var i = values.length;
while (i) {
  sum += values[--i];
}
ready
For with assignment - sum increment
for (var i = 0; match = values[i]; ++i) {
  sum += match;
}
ready
For with assignment, shorthand
for (var i = 0; match = values[i]; ++i)
  add(match);
ready
For with assignment, sum increment, shorthand
for (var i = 0; match = values[i]; ++i)
  sum += match;
ready
Inverse For with decrement-assignment, shorthand using add-function
for (var i = values.length; match = values[--i];)
  add(match);
ready
Inverse For with decrement-assignment, shorthand, sum up
for (var i = values.length; match = values[--i];)
  sum+=match
ready
Inverse For with assignment and decrement count, shorthand using add-function
for (var i = values.length-1; match = values[i]; --i)
  add(match);
ready
Inverse For with assignment and decrement count, shorthand, sum up
for (var i = values.length-1; match = values[i]; --i)
  sum+=match
ready

Revisions

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