ES6 Map vs Object properties

JavaScript performance comparison

Revision 31 of this test case created by bitshiffed

Preparation code

<script>
function randomString()
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    for( var i=0; i < 5; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var map = new Map();
  var obj = {};
  
  var map2 = new Map();
  var obj2 = {};
  var arr2 = [];
  
  for (var i=0; i < 1000; i++) {
     var key = randomString();
     var val = randomString();
     map2.set(key,val);
     obj2[key] = val;
     arr2.push(key);
  }
  

};
</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
map.set(key,val)
for(var i=0; i < 1000; i++) {
   var key = randomString();
   var val = randomString();
   map.set(key,val);
}
pending…
obj[key] = val;
for(var i=0; i < 1000; i++) {
   var key = randomString();
   var val = randomString();
   obj[key] = val;
}
pending…
Iterate Map
for(var [key, val] of map2) {
  var test = val;
}
pending…
Iterate Object
for(var key in obj2) {
  if(obj2.hasOwnProperty(key)) {
    var test = obj2[key];
  }
}
pending…
Map Lookup
for(var idx=0; idx < arr2.length; idx++) {
  var test = map2.get(key);
}
pending…
Object Lookup
for(var idx=0; idx < arr2.length; idx++) {
  var test = obj2[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