ES6 Map vs Object number properties

JavaScript performance comparison

Revision 15 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 arr = [];
  
  var map2 = new Map();
  var obj2 = Object.create(null);
  var arr2 = [];
  
  for (var i=0; i < 1000; i++) {
     var key = i;
     var val = i;
     map2.set(key,val);
     obj2[key] = val;
     arr2[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 = i;
   var val = i;
   map.set(key,val);
}
pending…
obj[key] = val;
for (var i=0; i < 1000; i++) {
   var key = i;
   var val = i;
   obj[key] = val;
}
pending…
Iterate Map
for (var key of map2.keys()) {
  var val = map2.get(key);
}
pending…
Iterate Object
var key;
for (key of Object.keys(obj2)) {
    var val = obj2[key];
}
pending…
Iterate Object using for...in
var key;
for (key in obj2) {
    var val = obj2[key];
}
pending…
arr[key] = val;
for (var i=0; i < 1000; i++) {
   var key = i;
   var val = i;
   arr[key] = val;
}
pending…
Iterate array
var key;
for (key of arr2) {
    var val = arr2[key];
}
pending…
Iterate array using forEach
var key;
arr2.forEach(function (val) {
    // ...
});
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