ES6 Map vs Object properties

JavaScript performance comparison

Revision 22 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 JSON.parse(JSON.stringify(text));
}
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var J = 10;
  var keys = [];
  
  for (var i=0; i < 1000; i++) {
    keys.push(randomString());
  }
  
  var mixedkeys = [];
  for (var i=0; i < 500; i++) {
    mixedkeys.push(randomString());
    mixedkeys.push(i);
  }
  
  var map = new Map();
  var obj = Object.create(null);
  obj.x = true;
  delete obj.x;
  
  for (var i = 0; i < 10; i++) {
    map.set(keys[i], i);
    obj[keys[i]] = i;
  }
  var x = 0;

};
</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)
var map = new Map();
for (var j = 0; j < J; j++)
for (var i = 0; i < keys.length; i++) {
  map.set(keys[i], i);
}
pending…
obj[key] = val;
var obj = Object.create(null);
obj.x = true;
delete obj.x;

for (var j = 0; j < J; j++)
for (var i = 0; i < keys.length; i++) {
  obj[keys[i]] = i;
}
pending…
map.get non-existant
for (var j = 0; j < J; j++)
for (var i = 0; i < keys.length; i++) {
  if (map.get(keys[i]) !== void 0) x++;
}
pending…
object get non-existant
for (var j = 0; j < J; j++)
for (var i = 0; i < keys.length; i++) {
  if (obj[keys[i]] !== void 0) x++;
}
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