Unrolled Loop Test

JavaScript performance comparison

Test case created by Brian Ford

Preparation code

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

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Rolled
var i, max = 0,
    len = data.length;

for (i = 0; i < len; i += 1) {
  if (data[i] > max) {
    max = data[i];
  }
}
pending…
Unrolled
var i, max = 0,
    len = data.length;

for (i = 0; i < len; i += 10) {
  if (data[i] > max) {
    max = data[i];
  }
  if (data[i + 1] > max) {
    max = data[i + 1];
  }
  if (data[i + 2] > max) {
    max = data[i + 2];
  }
  if (data[i + 3] > max) {
    max = data[i + 3];
  }
  if (data[i + 4] > max) {
    max = data[i + 4];
  }
  if (data[i + 5] > max) {
    max = data[i + 5];
  }
  if (data[i + 6] > max) {
    max = data[i + 6];
  }
  if (data[i + 7] > max) {
    max = data[i + 7];
  }
  if (data[i + 8] > max) {
    max = data[i + 8];
  }
  if (data[i + 9] > max) {
    max = data[i + 9];
  }
}
pending…
Unrolled from both sides
var i, j, max = 0,
    len = data.length;

for (i = 0, j = len - 1; i <= j; i += 1, j -= 1) {
  if (data[i] > max) {
    max = data[i];
  }
  if (data[j] > max) {
    max = data[j];
  }
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment