Array Performance (v60)

Revision 60 of this benchmark created on


Description

Manual array lookups vs. holey arrays.

Setup

//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);

Test runner

Ready to run.

Testing in
TestOps/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);
ready
Holey Array by Index
var id = 21346;
var result = a2[id];
ready
Object by Key
var id = '21346';
var result = o[id];
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.