Packed vs. holey arrays

JavaScript performance comparison

Revision 7 of this test case created by Volkan Ozcelik

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var packed_array = {'a':0, 'b':1, 'c':2, 'd':3, 'e':4, 'f':5, 'g':6, 'h':7, 'i':8, 'j':9, 'k':10};
   
    var holey_array1 = {'a':0, 'b':1, 'c':2, 'd':3, 'e':4, 'f':5, 'g':6, 'h':7, 'i':8, 'j':9, 'k':10};
    delete holey_array1.a;
   
    var holey_array2 = {'a':0, 'b':1, 'c':2, 'd':3, 'e':4, 'f':5, 'g':6, 'h':7, 'i':8, 'j':9, 'k':10};
    holey_array2.a = 3;
   
    function packed_sum() {
      var sum = 0;
      for (var key in packed_array) {
        sum += packed_array[key];
      }
    }
   
    function holey_sum1() {
      var sum = 0;
      for (var key in holey_array1) {
        sum += holey_array1[key];
      }
    }
   
    function holey_sum2() {
      var sum = 0;
      for (var key in holey_array2) {
        sum += holey_array2[key];
      }
    }
};
</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…

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

Volkan Ozcelik (revision owner) commented :

Using object literals instead of arrays has the same effect, unsurprisingly.

Add a comment