ES6 Map vs Object Get (v251)

Revision 251 of this benchmark created on


Description

(edited to make the string key actually contain a string portion--otherwise it was treating it as a number)

Setup

var mapStr = new Map();
  var objStr = {};
  var mapNum = new Map();
  var objNum = {};
  
  for (let i=0; i<10000 ; i++) {
     mapStr.set(`key_${i}`, Math.random());
     objStr[`key_${i}`] = Math.random();
     mapNum.set(i, Math.random());
     objNum[i] = Math.random();
  }

Test runner

Ready to run.

Testing in
TestOps/sec
[Number key] map.get
let temp;
let sum = 0;
for (let i=0; i < 10000; i++) {
   temp = mapNum.get(i);
   sum += temp;
}
console.log(`sum is ${sum}`);
ready
[String key] map.get
let temp;
let sum = 0;
for (let i=0; i < 10000; i++) {
   temp = mapStr.get(`key_${i}`);
   sum += temp;
}
console.log(`sum is ${sum}`);
ready
[Number key] obj[key]
let temp;
let sum = 0;
for (let i=0; i < 10000; i++) {
   temp = objNum[i];
   sum += temp;
}
console.log(`sum is ${sum}`);
ready
[String key] obj[key]
let temp;
let sum = 0;
for (let i=0; i < 10000; i++) {
   temp = objStr[`key_${i}`];
   sum += temp;
}
console.log(`sum is ${sum}`);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.