Array vs Object Access

JavaScript performance comparison

Revision 6 of this test case created by David

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];
    o= {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];
a[4];
a[6];
a[9];
pending…
Object access
o[3];
o[4];
o[6];
o[9];
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