Packed vs. holey arrays

JavaScript performance comparison

Revision 8 of this test case created by Hyzhak

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var packed_array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
   
    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 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
    holey_array4.splice(0, 1);
   
    var holey_array5 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
    holey_array4.splice(5, 1);
   
    var holey_array6 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
    holey_array4.splice(11, 1);
    holey_array4.splice(7, 1);
    holey_array4.splice(3, 1);
   
    function packed_sum() {
      var sum = packed_array.join();
    }
   
    function holey_sum1() {
      var sum = holey_array1.join();
    }
   
    function holey_sum2() {
      var sum = holey_array2.join();
    }
   
    function holey_sum3() {
      var sum = holey_array3.join();
    }
   
    function holey_sum4() {
      var sum = holey_array4.join();
    }
   
    function holey_sum5() {
      var sum = holey_array5.join();
    }
   
    function holey_sum6() {
      var sum = holey_array6.join();
    }
};
</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 / splice(0, 1)
holey_sum4();
pending…
Holey array sum 5 / splice(5, 1)
holey_sum5();
pending…
Holey array sum 6 / with 3 holes by splice
holey_sum6();
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:

1 comment

Hyzhak (revision owner) commented :

It seem that one splice especially at the beginning of array is now enough to convert it to the dictionary so it still have good performance

Add a comment