ES6 Map vs Object properties

JavaScript performance comparison

Revision 12 of this test case created by Stefan

Preparation code

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

    var textLength = Math.floor(Math.random() * possible.length);
    for( var i=0; i < textLength; 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 map3 = new Map();
  var map4 = new Map();
  var map5 = new Map();
  var obj2 = {};
  
  for (var i=0; i < 1000; i++) {
     var key = randomString();
     var val = randomString();
     map2.set(key,val);
     map3.set(key,val);
     map4.set(key,val);
     map5.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#keys
for (var key of map2.keys()) {
  var val = map2.get(key);
}
pending…
Iterate Map#entries_forof
for(var entry of map3.entries()) {
  var val = entry[1];
}
pending…
Iterate Map#entries_manual
var iter = map4.entries();
for(var i = iter.next(); !i.done; i = iter.next()) {
  var val = i.value[1];
}
pending…
Iterate Map#values
var iter = map5.values();
for(var i = iter.next(); !i.done; i = iter.next()) {
  var val = i.value;
}
pending…
Iterate Object
var key;
for (key in obj2) {
  if (obj2.hasOwnProperty(key)) {
    var val = obj2[key];
  }
}
pending…
Iterate Object 2
var key;
for (key in obj2) {
    var val = 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