javascript loop optimization (v7)

Revision 7 of this benchmark created on


Description

See here complete article:

http://conceptf1.blogspot.com/2014/01/javascript-best-practices-loop-optimization.html

Preparation HTML

<h3><a href='http://conceptf1.blogspot.com/2014/01/javascript-best-practices-loop-optimization.html'>JavaScript Best Practices : Loop Optimization </a></h3>

Setup

var i, max, fruits = ["apple", "banana", "orange", "mango"];

Test runner

Ready to run.

Testing in
TestOps/sec
counter & condition to break within loop
for (var i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}
ready
Cache condition to break
for (var i = 0, max = fruits.length; i < max; i++) {
  console.log(fruits[i]);
}
ready
cache counter and condition to break
for (i = 0, max = fruits.length; i < max; i++) {
  console.log(fruits[i]);
}
ready
Without increment (i++) operator
for (i = 0, max = fruits.length; i < max; i += 1) {
  console.log(fruits[i]);
}
ready
reverse loop
for (i = fruits.length; i -= 1;) {
  console.log(fruits[i]);
}
ready
while without decrement operator
i = fruits.length;
while (i -= 1) {
  console.log(fruits[i]);
}
ready
for in loop
for (var i in fruits) {
  console.log(i);
}
ready
forEach lambda
fruits.forEach(function(data) {
  console.log(data);
});
ready
forEach function reference
var log = function (data) {
  console.log(data);
};

fruits.forEach(log);
ready

Revisions

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

  • Revision 2: published by Zaheer Ahmed on
  • Revision 3: published by Zaheer Ahmed on
  • Revision 4: published by gotofritz on
  • Revision 5: published by ileadu on
  • Revision 6: published on
  • Revision 7: published on