Performance of Array vs. Object (v145)

Revision 145 of this benchmark created on


Description

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

Setup

var arr = [],
      i,
      obj = {};
    for (i = 0; i < 100000; i += 1) {
      var o = {
        payload: i
      };
      arr.push(o);
      obj[i] = o;
    }
    var arrLen = arr.length;
    var objLen = Object.keys(obj).length;

Test runner

Ready to run.

Testing in
TestOps/sec
Array Performance
var sum = 0;
 
ready
Object Performance
var len = 0,
i = 0;
while (true) {
 if (obj[i]) len+=1;
 i++
}
ready
Array performance (test fetching length)
var len = 0;
for (var i in obj) {
len += 1;
}
ready
Object performance (test fetching length)
var len = Object.keys(obj).length;
 
ready

Revisions

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