Array Performance (v157)

Revision 157 of this benchmark created on


Description

Manual array lookups vs. holey arrays.

Setup

var i, id;
    var source = {};
window.lastId = null
    for (i = 0; i < 10000; i++) {
        id = Math.round(Math.random() * 100000);
        source[id] = { id:id, name: 'name1' };
        window.lastId = id;
    }

    var a1 = [];
    for (var key in source) {
        a1.push(source[key]);
    }

    var a2 = [];
    for (var key in source) {
        a2[key] = source[key];
    }

    var o = {};
    for (var key in source) {
        o[""+key+""] = source[key];
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Manual Array Lookup
var id = lastId;
for (var i = 0; i < a1.length; i++) {
  if (a1[i].id === id) result = a1[i];
}
 
ready
Holey Array by Index
var id = lastId;
var result = a2[id];
ready
Object by Key
var id = ""+lastId+"";
var result = o[id];
 
ready

Revisions

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