Packed vs. holey arrays

JavaScript performance comparison

Revision 10 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var LargeObj = function(num) { this.num = num; };
   
    var packed_array = [new LargeObj(0), new LargeObj(1), new LargeObj(2), new LargeObj(3), new LargeObj(4), new LargeObj(5), new LargeObj(6), new LargeObj(7), new LargeObj(8), new LargeObj(9)];
   
    var holey_array1 = [new LargeObj(0), new LargeObj(1), new LargeObj(2), new LargeObj(3), new LargeObj(4), new LargeObj(5), new LargeObj(6), new LargeObj(7), new LargeObj(8), new LargeObj(9)];
    delete holey_array1[1];
   
    var holey_array2 = [new LargeObj(0), new LargeObj(1), new LargeObj(2), new LargeObj(3), new LargeObj(4), new LargeObj(5), new LargeObj(6), new LargeObj(7), new LargeObj(8)];
    holey_array2[13] = new LargeObj(13);
   
    var holey_array3 = [new LargeObj(0), new LargeObj(1), new LargeObj(2), new LargeObj(3), new LargeObj(4), new LargeObj(5), new LargeObj(6), new LargeObj(7), /* hole */, new LargeObj(9)];
   
    var holey_array4 = [new LargeObj(0), new LargeObj(1), new LargeObj(2), new LargeObj(3), new LargeObj(4), new LargeObj(5), new LargeObj(6), new LargeObj(7), new LargeObj(8), new LargeObj(9)];
    holey_array1[1] = false;
   
    function packed_sum() {
      var sum = 0;
      for (var i = 0; i < packed_array.length; i++) {
        if (packed_array[i]) {
          sum += packed_array[i].num;
        }
      }
    }
   
    function holey_sum1() {
      var sum = 0;
      for (var i = 0; i < holey_array1.length; i++) {
        if (holey_array1[i]) {
          sum += holey_array1[i].num;
        }
      }
    }
   
    function holey_sum2() {
      var sum = 0;
      for (var i = 0; i < holey_array2.length; i++) {
        if (holey_array2[i]) {
          sum += holey_array2[i].num;
        }
      }
    }
   
    function holey_sum3() {
      var sum = 0;
      for (var i = 0; i < holey_array3.length; i++) {
        if (holey_array3[i]) {
          sum += holey_array3[i].num;
        }
      }
    }
   
    function holey_sum4() {
      var sum = 0;
      for (var i = 0; i < holey_array4.length; i++) {
        if (holey_array4[i]) {
          sum += holey_array4[i].num;
        }
      }
    }
};
</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
Packed array sum
packed_sum();
pending…
Holey array sum 1
holey_sum1();
pending…
Holey array sum 2
holey_sum2();
pending…
Holey array sum 3
holey_sum3();
pending…
Holey array sum 4
holey_sum4();
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