ES6 Map vs Object properties

JavaScript performance comparison

Revision 97 of this test case created by Balrob and last updated

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.2.1/core.min.js"></script>
<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 < 100000; i++) {
   preComputedRandomKeys[i] = randomString();
   preComputedRandomVals[i] = randomString();
}
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var map = new Map();
  var obj = {};
  
  for (var i=0; i < 100000; i++) {
     map.set(preComputedRandomKeys[i], preComputedRandomVals[i]);
     obj[preComputedRandomKeys[i]] = preComputedRandomVals[i];
  }
  

};
</script>

Preparation code output

<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 < 100000; i++) { preComputedRandomKeys[i] = randomString(); preComputedRandomVals[i] = randomString(); } </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 < 100000; i++) {
   var key = preComputedRandomKeys[i];
   var val = preComputedRandomVals[i];
   map.set(key, val);
}
pending…
obj[key] = val;
for (var i=0; i < 100000; i++) {
   var key = preComputedRandomKeys[i];
   var val = preComputedRandomVals[i];
   obj[key] = val;
}
pending…
Iterate Map
for (var i = 0, entries = map.entries(); i < entries.length; i++) {
   let entry = entries[i];
   let key = entry[0];
   let val = entry[1];
}
pending…
Iterate Object
var key;
for (key in obj) {
  if (obj.hasOwnProperty(key))
    var val = obj[key];
}
pending…
val = map.get(key)
for (var i=0; i < 100000; i++) {
   var val = map.get(preComputedRandomKeys[i]);
}
pending…
val = obj[key]
for (var i=0; i < 100000; i++) {
   var val = obj[preComputedRandomKeys[i]];
}
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