Codeschool loop optimization (v12)

Revision 12 of this benchmark created on


Setup

var obj = {
      items: []
    };
    obj.items = new Array(10000);
    
    function process(el) {
      var i = 500;
      while (i--) {
        el = i;
      }
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Basic for loop
for (var i = 0; i < obj.items.length; i++) {}
ready
For loop, but caching the length.
for (var i = 0, len = obj.items.length; i < len; i++) {}
ready
While loop that imitates a for loop.
var i = 0;
while (i < obj.items.length) {
  i++;
}
ready
While loop that imitates a for loop, caching the length.
var i = 0;
var len = obj.items.length;
while (i < len) {
  i++;
}
ready
While loop in reverse, simplifying the test condition.
var i = obj.items.length;
while (i--) {}
ready
forEach
obj.items.forEach(function(i) {

});
ready

Revisions

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