short for vs unrolled if

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arrays = [
      [{
        id: 2,
        name: "banana",
        x: 10
      }, {
        id: 4,
        name: "apple",
        x: 1,
        y: 1
      }, {
        id: 1,
        name: "squash",
        x: 10
      }],
      [{
        id: 4,
        name: "apple",
        x: 1,
        y: 1
      }, {
        id: 3,
        name: "grape",
        x: 10,
        y: 1
      }],
      [{
        id: 2,
        name: "banana",
        x: 10
      }],
      [],
      [],
      [{
        id: 4,
        name: "apple",
        x: 1,
        y: 1
      }, {
        id: 3,
        name: "grape",
        x: 10,
        y: 1
      }],
      [{
        id: 2,
        name: "banana",
        x: 10
      }],
    ];
    var alen = arrays.length;
    var i = 0;
    function get_array() {
      if (i == alen) {
        i = 0;
      }
      return arrays[i++];
    }
   
    function do_something(array) {
      if (array.id > 2 && array.x !== undefined && array.y !== undefined) {
        var a = array.x * array.y;
      }
    }
};
</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
loop
var array = get_array();
for (var i = 0, len = array.length; i != len; i++) {
  do_something(array);
}
pending…
unrolled if
var array = get_array();
var len = array.length;
if (len !== 0) {
  if (len == 1) {
    do_something(array[0]);
  } else if (len == 2) {
    do_something(array[0]);
    do_something(array[1]);
  } else {
    do_something(array[0]);
    do_something(array[1]);
    do_something(array[2]);
  }
}
pending…

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

Compare results of other browsers

0 comments

Add a comment