Performance of Array vs. Object

JavaScript performance comparison

Revision 138 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function makeid(len){
      var text = "";
      var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  
      for( var i=0; i < len; i++ )
          text += possible.charAt(Math.floor(Math.random() * possible.length));
  
      return text;
  }
  
  var arr1 = [], arr2 =[], obj = {};
  
  for(i = 0; i < 100; i += 1) {
  var str1 = makeid(50);
  var str2 = makeid(50);     
  arr1.push(str1);
  arr2.push(str2);
  obj[str1] = str2;
  }
  
  var key = arr1[Math.floor(100*Math.random())];

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Array Performance
for(i = 0; i < 100; i += 1) {
var index = arr1.indexOf(key);
var str = arr2[index];
}
 
pending…
Object Performance
for(i = 0; i < 100; i += 1) {
var str = obj[key];
}
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.

0 Comments