Performance of Array vs. Object

JavaScript performance comparison

Revision 70 of this test case created

Info

After seeing http://jsperf.com/javascript-associative-vs-non-associative-arrays, I thought the test could be improved.

Made lengths consistent and fattened element number to 100,000. - KAS

Initialized vars in the setup, added "Object While" loop - bmcgin

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = [],
      i,
      obj = {};
    for (i = 0; i < 100000; i += 1) {
      var o = {
        payload: i
      };
      arr.push(o);
      obj[i] = o;
    }
   
    var sum = 0;
    var keys = Object.keys(obj);
    var len = keys.length;
    var i = 0;
};
</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 Performance
for (var x = 0; x < 100000; ++x) {
  sum += arr[x].payload
}
pending…
Object Performance
for (var x in obj) {
  sum += obj[x].payload;
}
pending…
Object Performance using known length
for (var x = 0; x < 100000; ++x) {
  sum += obj[x].payload
}
pending…
Using Object.keys()
for (var x = 0; x < keys.length; ++x) {
  sum += obj[keys[x]].payload
}
pending…
Array Performance known length
for (var x = 0; x < 100000; ++x) {
  sum += arr[x].payload
}
pending…
Object Keys - While
i = 0;
while (i < len) {
  sum += obj[keys[i]].payload
  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