Object Loops - String Index vs. Number Index

JavaScript performance comparison

Revision 4 of this test case created by Andrew

Info

Which is better performance on large loops, objects with string indexes or number indexes?

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var the_digital_object, the_stringy_object, i, len, result;
   
    the_digital_object = {};
    the_stringy_object = {};
   
    for (i = 0; i< 10000; i++) {
      the_digital_object[i] = "foobar";
      the_stringy_object["index" + i] = "foobar";
    }
};
</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
Digital Indexes
for (i in the_digital_object) {
  result = the_digital_object[i];
}
pending…
Stringy Indexes
for (i in the_stringy_object) {
  result = the_stringy_object[i];
}
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