Array Performance (v125)

Revision 125 of this benchmark created on


Description

Manual array lookups vs. holey arrays.

Setup

var a1 = [
      {id: 12, name: 'name1'}, 
      {id: 16, name: 'name1'}, 
      {id: 29, name: 'name1'}, 
      {id: 32, name: 'name1'}
    ];

    var a2 = [];
    a2[12] = {id: 12, name: 'name1'};
    a2[16] = {id: 16, name: 'name1'};
    a2[29] = {id: 29, name: 'name1'};
    a2[32] = {id: 32, name: 'name1'};

    var o1 = {};
    o1['12'] = {id: 12, name: 'name1'};
    o1['16'] = {id: 16, name: 'name1'};
    o1['29'] = {id: 29, name: 'name1'};
    o1['32'] = {id: 32, name: 'name1'};

    var o2 = {};
    o2[12] = {id: 12, name: 'name1'};
    o2[16] = {id: 16, name: 'name1'};
    o2[29] = {id: 29, name: 'name1'};
    o2[32] = {id: 32, name: 'name1'};

Test runner

Ready to run.

Testing in
TestOps/sec
Manual Array Lookup ==
var a1Length = a1.length, result = false, id = 29;

for (var i = 0; i < a1Length && !result; i++)
  if (a1[i].id == id) result = a1[i];
ready
Holey Array by Index
var id = 29;
var result = a2[id];
ready
Object by string Key
var id = '29';
var result = o1[id];
ready
Manual Array Lookup ===
var a1Length = a1.length, result = false, id = 29;

for (var i = 0; i < a1Length && !result; i++) 
  if (a1[i].id === id) result = a1[i];
ready
Object by numeric Key
var id = 29;
var result = o2[id];
ready

Revisions

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