Packed vs. holey arrays

JavaScript performance comparison

Revision 5 of this test case created by Yu-Cheng Chuang

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var i;
   
    var packed_array1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
   
    var packed_array2 = new Array(11);
    for (i = 0; i < 11; i++ ) {
      packed_array2[i] = i;
    }
   
    var holey_array1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    delete holey_array1[0];
   
    var holey_array2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    holey_array2[13] = 0;
   
    var holey_array3 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* hole */, 10];
   
    var holey_array4 = new Array(11);
   
    // only fill the first 10 elements
    for (i = 0; i < 10; i++) {
      holey_array4[i] = i;
    }
   
    function sum(array) {
      var sum = 0;
      for (var i = 0; i < array.length; i++) {
        if (typeof array[i] !== "undefined") {
          sum += array[i];
        }
      }
    }
   
};
</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 1
sum(packed_array1);
pending…
Holey array sum 1
sum(holey_array1);
pending…
Holey array sum 2
sum(holey_array2);
pending…
Holey array sum 3
sum(holey_array3);
pending…
Holey array sum 4
sum(holey_array4);
pending…
Packed array sum 2
sum(packed_array2);
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