Array Performance (v101)

Revision 101 of this benchmark created by tuan on


Description

Manual array lookups vs. holey arrays.

Setup

var a1 = [];
    for (var i = 0; i < 2; i++) {
       a1[i] = {
         id: i,
         name: 'test'
       };
    }

    var a2 = {};
    for (var i = 0; i < 2; i++) {
       a2[i] = {
         id: 'a' + i,
         name: 'test'
       };
    }

    var a3 = {};
    for (var i = 0; i < 2; i++) {
       a3['a' + i] = {
         id: 'a' + i,
         name: 'test'
       };
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Array query
var id = 1;
for (var i = 0; i < a1.length; i++) {
  if (a1[i].id === id) result = a1[i];
}
ready
Obj query
var id = 1;
var result = a2[id];
ready
obj (key is string) query
var id = 'a1';
var result = a3[id];
ready

Revisions

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