Array Performance (v130)

Revision 130 of this benchmark created on


Description

Manual array lookups vs. holey arrays.

Setup

var a1 = [{id: 29938, name: 'name1'}, {id: 32994, name: 'name2'}];

    var a2 = [];
    a2[29938] = {id: 29938, name: 'name1'};
    a2[32994] = {id: 32994, name: 'name2'};

    var o = {};
    o['29938'] = {id: 29938, name: 'name1'};
    o['32994'] = {id: 32994, name: 'name2'};

    var o2 = {};
    o2['_29938'] = {id: 29938, name: 'name1'};
    o2['_32994'] = {id: 32994, name: 'name2'};

    var a3 = [];
    a3[29938] = {id: 29938, name: 'name1'};
    a3[32994] = {id: 32994, name: 'name2'};

    var a4 = [];
    a4[29938] = {id: 29938, name: 'name1'};
    a4[32994] = {id: 32994, name: 'name2'};

Test runner

Ready to run.

Testing in
TestOps/sec
Manual Array Lookup
//This test just uses a 2 length array
var id = 29938;
for (var i = 0; i < a1.length; i++) {
  if (a1[i] && a1[i].id == id) {
    var result = a1[i];
  }
}
 
ready
Holey Array by Index
var id = 29938;
var result = a2[id];
ready
Object by Key
var id = '29938';
var result = o[id];
ready
Object by Property
var result = o2._29938;
ready
Array Reverse While Lookup
var id = 29938;
var count = a3.length;
while (count--) {
  if (a3[count] && a3[count].id == id) {
    var result = a3[count];
  }
}
ready
Array For Lookup
var id = 29938;
for (var i = 0; i < a4.length; i++) {
  if (a4[i] && a4[i].id == id) {
    var result = a4[i];
  }
}
 
ready

Revisions

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