ES6 Map vs Object properties

JavaScript performance comparison

Revision 42 of this test case created by

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 = Object.create(null);
  
  var map2 = new Map();
  var obj2 = Object.create(null);
  
  for (var i=0; i < 1000; i++) {
     var key = randomString();
     var val = randomString();
     map2.set(key,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
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 of)
var keys = map2.keys();
for (var key of keys) {
  var val = map2.get(key);
}
pending…
Iterate Map (for)
var keys = [...map2.keys()];
var len = keys.length;
for (var i = 0; i < len; i++) {
  var val = map2.get(keys[i]);
}
pending…
Iterate Object
var key;
for (key in obj2) {
    var val = obj2[key];
}
pending…
Iterate Map (keys forEach)
[...map2.keys()].forEach(function(key) {
  var val = map2.get(key);
});
pending…
Iterate Map (Map forEach)
map2.forEach(function(value, key) {
  var val = value;
});
pending…
Iterate Map (item of map)
for (var item of map2) {
  var val = item[1];
}
pending…
Iterate Map (k, v of map)
for (var [key, value] of map2) {
  var val = value;
}
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