Loop iteration (length comparison variations) (v9)

Revision 9 of this benchmark created by Raymond on


Description

Comparison of different ways to treat the length of an array when iterating the array.

Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.0.0-rc.3/lodash.min.js"></script>

Setup

window.list = [];
    var x = 100;
    for (var y = 0; y < x; y++) {
      window.list[y] = true;
    }

Teardown


    delete window.list;
  

Test runner

Ready to run.

Testing in
TestOps/sec
Using list.length
for (var i = 0; i < window.list.length; i++) {
  var fruit = window.list[i];
}
ready
Using static len - outside for declr.
var len = window.list.length;
for (var i = 0; i < len; i++) {
  var fruit = window.list[i];
}
ready
Using static len - inside for declr.
for (var i = 0, len = window.list.length; i < len; i++) {
  var fruit = window.list[i];
}
ready
Reverse Loop (for)
for (var i = window.list.length; i--;) {
  var fruit = window.list[i];
}
ready
Reverse Loop (while)
var i = window.list.length;
while (i--) {
  var fruit = window.list[i];
}
ready
Reverse Loop (for) w/ outside declaration & --
var i = window.list.length;
for (; i--;) {
  var fruit = window.list[i];
}
ready
Reverse Loop (for) w/ outside declaration & >= 0
var i = window.list.length,
    fruit;
for (; i >= 0; i--) {
  fruit = window.list[i];
}
ready
Destructive array - shift
var list = window.list.slice(0),
    fruit = list.shift();
while (fruit) {
  fruit = list.shift();
}
ready
Destructive array - pop
var list = window.list.slice(0),
    fruit = list.pop();
while (fruit) {
  fruit = list.pop();
}
ready
Destructive array - pop w/ reverse
var list = window.list.slice(0).reverse(),
    fruit = list.pop();
while (fruit) {
  fruit = list.pop();
}
ready
_.times
_.times(window.list.length, function (i) {

  var fruit = window.list[i];
});
ready
_.each
_.each(window.list, function (item) {

  var fruit = item;
});
ready

Revisions

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