Array vs Object Access (v16)

Revision 16 of this benchmark created by Matt C on


Description

JavaScript arrays are basically just objects with integer property names. Is there any performance difference when accessing an array vs an object?

Setup

var a = [];
    var o = {};
    for(var i=0; i<2000; i++) {
        a.push("someValue");
        o["h" + i.toString()] = "someValue";
    }
    o["a"] = "someValue";
    o["b"] = "someValue";
    o["c"] = "someValue";
    o["d"] = "someValue";

Test runner

Ready to run.

Testing in
TestOps/sec
Array access
a[3];
a[4];
a[6];
a[9];
ready
Object access
o.a;
o.b;
o.c;
o.d;
ready
Object access 2
o["a"];
o["b"];
o["c"];
o["d"];
ready
Object access Numbers
o["h5"];
o["h6"];
o["h7"];
o["h8"];
ready

Revisions

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