FOR LOOPS test (v5)

Revision 5 of this benchmark created by getsetbro on


Description

Difference between for/in, classical for loop and classical loop with caching.

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>

Setup

var myArray = [];
    for (var i = 0; i < 1024; i++) {
      myArray.push(i);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery each 1
jQuery.each(myArray, function(elem, i) {
  myArray[i] = myArray[i]++;
});
ready
for loop (no caching)
for (var i = 0; i < myArray.length; i++) {
  myArray[i] = myArray[i]++;
}
ready
for loop (with caching)
for (var i = 0, l = myArray.length; i < l; i++) {
  myArray[i] = myArray[i]++;
}
ready
jQuery.each (for comparison)
jQuery.map(myArray, function(elem, i) {
  return elem++;
});
ready
for loop (no caching) - caching length
var length = myArray.length;
for (var i = 0; i < length; i++) {
  myArray[i] = myArray[i]++;
}
ready
for loop (with caching) - caching length
var length = myArray.length;
for (var i = 0, l = length; i < l; i++) {
  myArray[i] = myArray[i]++;
}
ready
for loop (with caching) - caching length, caching i
var len = myArray.length,
    i;
for (i = 0, l = len; i < l; i++) {
  myArray[i] = myArray[i]++;
}
ready

Revisions

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