Array Performance

JavaScript performance comparison

Revision 60 of this test case created

Info

Manual array lookups vs. holey arrays.

Preparation code

<script>
  Benchmark.prototype.setup = function() {

    //Using an object constructor will make the calculations go faster in chromes v8 engine
    function userConstructor (id, name1) {
        this.id = id;
        this.name1 = name1;
    }

    //Cached to speed up the object constructor as we are testing lookup speed not the constructor speed.
    var name1 = 'Some Random Name';

    var a1 = [new userConstructor(29938, name1),
              new userConstructor(32994, name1),
              new userConstructor(54235, name1),
              new userConstructor(43412, name1),
              new userConstructor(21346, name1)
             ];

    var a2 = [];
    a2[29938] = new userConstructor(29938, name1);
    a2[32994] = new userConstructor(32994, name1);
    a2[54235] = new userConstructor(54235, name1);
    a2[43412] = new userConstructor(43412, name1);
    a2[21346] = new userConstructor(21346, name1);

    var o = {};
    o['29938'] = new userConstructor(29938, name1);
    o['32994'] = new userConstructor(32994, name1);
    o['54235'] = new userConstructor(54235, name1);
    o['43412'] = new userConstructor(43412, name1);
    o['21346'] = new userConstructor(21346, name1);
  };
</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
Manual Array Lookup
function getNameById(id) {
  for (var i = 0; i < a1.length; i++) {
    if (a1[i].id === id) {
      return a1[i].name1;
    }
  }
}

var id = 21346;
var result = getNameById(id);
pending…
Holey Array by Index
var id = 21346;
var result = a2[id];
pending…
Object by Key
var id = '21346';
var result = o[id];
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