Different kinds of loop (v4)

Revision 4 of this benchmark created by Jamie Mason on


Description

This revision is a subset of the full suite at revision 2 with only the 8 most performant loops and each test limited to a max time of 2 seconds.

This is because https://browserlab.adobe.com has a max timeout of 20 seconds before it takes a screenshot. If we want to use that tool to help test many browsers at once it has to be complete within that time.

There is also http://browsercam.com, which has a max timeout of 30 seconds.

Please note the scope of this test suite is to only test native JavaScript loop constructs, not utilities such as $.each, _.each etc. They will be covered by other suites.

This test suite is to provide data for this JavaScript loop optimising GitHub project, more information is available on the GitHub page, thanks.

Jamie Mason (@GotNoSugarBaby)

Preparation HTML

<script>
    Benchmark.options.maxTime = 2; 

    var arr = [], obj = {};

    (function ()
    {
      for( var i = 0; i < 1000; i++ ) {
          arr[i] = 'value' + i;
          obj[i] = 'value' + i;
      }    
    }());

    function someFn (ix)
    {
      return ix * 5 + 1 / 3 * 8;
    }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
while, counting down
var i = arr.length;
do
{
    someFn(i);
    i--;
}
while (i > 0);
ready
while, counting down (2)
var i = arr.length - 1;
do
{
    someFn(i);
}
while (i--);
ready
while, counting down (3)
var i = arr.length;
if (i > 0)
{
    do
    {
        someFn(i);
    }
    while (--i); // i must be greater than 0 here
}
ready
for, counting down
for (var i = arr.length; i > 0; i--) {
    someFn(i);
}
ready
while i < arr.length (with caching)
var i = 0, max = arr.length;
do 
{
    someFn(i);
    i++;
}
while (i < max);
ready
for i < arr.length (without caching)
for( var i = 0; i < arr.length; i++ ) {
    someFn(i);
}
ready
for i < arr.length (with caching)
for( var i = 0, max = arr.length; i < max; i++ ) {
    someFn(i);
}
ready
while i < arr.length (without caching)
var i = 0;
do 
{
    someFn(i);
    i++;
}
while (i < arr.length);
ready

Revisions

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