ES6 Map vs Object properties

JavaScript performance comparison

Revision 186 of this test case created by Ryan Carniato

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 objs = [];
  
  var map2 = new Map();
  var map3 = new Map();
  var obj2 = {};
  
  for (var i=0; i < 1000; i++) {
     var key = randomString();
     var val = randomString();
     var objK;
     objs.push(objK = {k: randomString()});
     map2.set(key,val);
     map3.set(objK, val);
     obj2[key] = val;
  }

};
</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
Iterate Object
var keys = Object.keys(obj2);
for (let i = 0, len = keys.length; i < len; i++) {
  var val = obj2[keys[i]];
}
pending…
map.set(obj,val)
for (var i=0; i < 1000; i++) {
   var key = objs[i];
   var val = randomString();
   map.set(key,val);
}
pending…
Iterate Object Map
for (var key of map3.keys()) {
  var val = map3.get(key);
}
pending…
map.set(key,val)
for (var i=0; i < 1000; i++) {
   var key = randomString();
   var val = randomString();
   map.set(key,val);
}
pending…
Iterate Map
for (var key of map2.keys()) {
  var val = map2.get(key);
}
pending…
obj[key] = val;
for (var i=0; i < 1000; i++) {
   var key = randomString();
   var val = randomString();
   obj[key] = val;
}
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