Array vs Object

JavaScript performance comparison

Revision 3 of this test case created

Preparation code

<script>
  var len = 10000;
 
  function addArrayItem(arr) {
     for (var i=1;i<len;i++) {
          arr.push(i);
     }
  }
  function addObjItem(obj) {
     for (var i=1;i<len;i++) {
          obj[i.toString()]=true;
     }
  }
  var arr=[],obj={};
  function setup() {
     addArrayItem(arr);
     arrObjItem(obj);
  }
 
  function rndIndex() {
    return ~~(Math.random() * len);
  }
</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
Access Speed - Arr
var x = arr[rndIndex()];
pending…
Access Speed - Obj
var x = obj['' + rndIndex()];
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