Array vs Object Setter

JavaScript performance comparison

Revision 9 of this test case created

Info

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

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    a= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    a2 = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
    o= {"a":1, "b":2, "c":3, "d":4, "e":5, "f": 6, "g": 7, "h":8, "i":9, "j": 10};
    o2= {"1":1, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "10":10};
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Array access
a[3] = 15;
a[4] = 15;
a[6] = 15;
a[9] = 15;
pending…
Object access
o.d = 15;
o.e = 15;
o.g = 15;
o.i = 15;
pending…
Object access 2
o["d"] = 15;
o["e"] = 15;
o["g"] = 15;
o["i"] = 15;
pending…
Object access Numbers
o2["3"] = 15;
o2["4"] = 15;
o2["6"] = 15;
o2["9"] = 15;
pending…
Array access - Typed
a2[3] = 15;
a2[4] = 15;
a2[6] = 15;
a2[9] = 15;
pending…
Object access Numbers 2
o2[3] = 15;
o2[4] = 15;
o2[6] = 15;
o2[9] = 15;
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment