ES6 Map vs Object properties

JavaScript performance comparison

Revision 144 of this test case created by ZAlexz

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;
}

var preComputedRandomKeys = [];
var preComputedRandomVals = [];

for (var i=0; i < 1000; i++) {
   preComputedRandomKeys[i] = randomString();
   preComputedRandomVals[i] = randomString();
}
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var map = new Map(), key, val, dummy, keys, values;
  var obj = {};
  
  var map2 = new Map();
  var obj2 = {};
  
  for (var i=0; i < 1000; i++) {
     map2.set(preComputedRandomKeys[i], preComputedRandomVals[i]);
     obj2[preComputedRandomKeys[i]] = preComputedRandomVals[i];
  }

};
</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 Map - iterable
values = map2.values();

for (var i=0, len = map2.size; i < len; ++i) {
   val = values.next().value; // .next() returns obj {value:..., done:false}
}
pending…
Iterate Map - map.forEach( cb )
map2.forEach( (key, val) => { dummy = val; });
pending…
Iterate Map
for (var key of map2.keys()) {
  val = map2.get(key);
}
pending…
Iterate Object
for (key in obj2) {
  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