Unrolled Loop Test (v7)

Revision 7 of this benchmark created on


Preparation HTML

<script>
  var data = (function() {
  
    var arr = [],
        n;
  
    for (n = 0; n < 1003; n += 1) {
      arr.push(Math.random());
    }
  
    return arr;
  }());
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Rolled
var max = 0,i,l;

for (i = 0, l = data.length; i < l; i ++) {
  if (data[i] > max) {
    max = data[i];
  }
}
ready
Rolled Reverse
var max = 0,i;
i = data.length;

while (i--) {
  if (data[i] > max) {
    max = data[i];
  }
}
ready
Duff Unrolled
var iterations = Math.ceil(data.length / 8);
var startAt = data.length % 8;
var i = 0, max = 0;

do {
        switch(startAt){
                case 0: 
  if (data[i] > max) {
    max = data[i];
  }
  i++;
                case 7: 
  if (data[i] > max) {
    max = data[i];
  }
  i++;
                case 6: 
  if (data[i] > max) {
    max = data[i];
  }
  i++;
                case 5: 
  if (data[i] > max) {
    max = data[i];
  }
  i++;
                case 4: 
  if (data[i] > max) {
    max = data[i];
  }
  i++;
                case 3: 
  if (data[i] > max) {
    max = data[i];
  }
  i++;
                case 2: 
  if (data[i] > max) {
    max = data[i];
  }
  i++;
                case 1: 
  if (data[i] > max) {
    max = data[i];
  }
  i++;
        }
        startAt = 0;
} while (--iterations > 0);
ready
Leftover unrolled
var iterations = Math.floor(data.length / 8);
var leftover = data.length % 8;
var i = 0, max = 0;

if (leftover > 0){
        do {
                 
  if (data[i] > max) {
    max = data[i];
  }
        } while (--leftover > 0);
}

do {
         
  if (data[i] > max) {
    max = data[i];
  }      
  if (data[i] > max) {
    max = data[i];
  } 
  if (data[i] > max) {
    max = data[i];
  } 
  if (data[i] > max) {
    max = data[i];
  } 
  if (data[i] > max) {
    max = data[i];
  } 
  if (data[i] > max) {
    max = data[i];
  } 
  if (data[i] > max) {
    max = data[i];
  } 
  if (data[i] > max) {
    max = data[i];
  }
} while (--iterations > 0);
ready
Leftover Unrolled without inline call
var values = data;
var iterations = Math.floor(values.length / 8);
var leftover = values.length % 8;
var i = 0, max = 0;
function process(elem) {
  if (elem > max) {
    max = elem;
  }
}

if (leftover > 0){
        do {
                process(values[i++]);
        } while (--leftover > 0);
}

do {
        process(values[i++]);
        process(values[i++]);
        process(values[i++]);
        process(values[i++]);
        process(values[i++]);
        process(values[i++]);
        process(values[i++]);
        process(values[i++]);
} while (--iterations > 0);
ready

Revisions

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