Native Loop Performance (v3)

Revision 3 of this benchmark created by Greg on


Description

Testing which method of looping through an array with 285 items is fastest.

Preparation HTML

<script>
  var arr = [1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9, 1, 2, , 3, 4, 5, 6, 7, 8, 9]
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
for()
for (var i = 0; i < arr.length; i++) {}
ready
for() caching length
for (var i = 0, length = arr.length; i < length; i++) {}
ready
while() acting like for()
var i = 0;
while (i < arr.length) {
 i++
}
ready
while() acting like for(), caching length
var i = 0,
    length = arr.length;
while (i < length) {
 i++
}
ready
while() in decrement
var length = arr.length;
while (length--) {}
ready
for() decrement
for (var i = arr.length - 1; i >= 0; i--) {}
ready

Revisions

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