ES6 Map vs Object properties

JavaScript performance comparison

Revision 201 of this test case created by Alex Mitchell

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var map = new Map();
  var obj = Object.create(null);
  
  var map2 = new Map();
  var obj2 = Object.create(null);
  var usedKeys = new Array(1000);
  for (var i=0; i < 1000; i++) {
     var key = 'key' + i;
     var val = 'val' + i;
     usedKeys.push(key);
     map2.set(key,val);
     obj2[key] = val;
  }
  
  var unusedKeys = new Array(100);
  for (var i = 1000; i < 1100; i++) {
    unusedKeys.push('key' + i)
  }
  
  var presetMapKeys = [
    ['key1', 'val1'],
    ['key2', 'val2'],
    ['key3', 'val3'],
    ['key4', 'val4'],
    ['key5', 'val5']
  ];
  
  var presetObjectKeys = {
    'key1': 'val1',
    'key2': 'val2',
    'key3': 'val3',
    'key4': 'val4',
    'key5': 'val5',
  }

};
</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
add properties to Map
for (i = 0; i < unusedKeys.length; i++) {
  map2.set(unusedKeys[i], i);
}
pending…
delete property from Object
for (i = 0; i < usedKeys.length; i++) {
  delete obj2[usedKeys[i]]
}
pending…
reassign properties Map
for (i = 0; i < usedKeys.length; i++) {
  map2.set(usedKeys[i], i);
}
pending…
initialize Map with values
let newMap = new Map(presetMapKeys);
pending…
initialize Object with values
let newObj = {
  key1: presetObjectKeys.key1,
  key2: presetObjectKeys.key2,
  key3: presetObjectKeys.key3,
  key4: presetObjectKeys.key4,
  key5: presetObjectKeys.key5,
};
pending…
initialize Map
let newMap = new Map();
pending…
delete property from Map
for (i = 0; i < usedKeys.length; i++) {
  map2.delete(usedKeys[i]);
}
pending…
initialize Object
let newObj = {};
pending…
shallow clone Map
let newMap = new Map(map2);
pending…
initialize Object (Object.create))
let newObj = Object.create(null);
pending…
set property Object
for (i = 0; i < usedKeys.length; i++) {
  obj[usedKeys[i]] = i;
}
pending…
set property Map
for (i = 0; i < usedKeys.length; i++) {
  map.set(usedKeys[i], i);
}
pending…
reassign properties Object
for (i = 0; i < usedKeys.length; i++) {
  obj2[usedKeys[i]] = i;
}
pending…
add properties to Object with existing keys
for (i = 0; i < unusedKeys.length; i++) {
  obj2[unusedKeys[i]] = i;
}
pending…
shallow clone Object (Object.assign)
let newObj = Object.assign({}, obj2);
pending…
shallow clone Object (spread)
let newObj = { ...obj2 };
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