ES6 Map vs Object Get (v252)

Revision 252 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 = {};
var strKeys = [];
var numKeys = [];
  
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();
   numKeys[i] = i;
   strKeys[i] = `key_${i}`;
}

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
[known str key] map.get
let temp;
let sum = 0;
for (let i=0; i < 10000; i++) {
   temp = mapStr.get(strKeys[i]);
   sum += temp;
}
console.log(`sum is ${sum}`);
ready
[known str key] obj[key]
let temp;
let sum = 0;
for (let i=0; i < 10000; i++) {
   temp = objStr[strKeys[i]];
   sum += temp;
}
console.log(`sum is ${sum}`);
ready
[lookup num key to compare with known str key] obj[key]
let temp;
let sum = 0;
for (let i=0; i < 10000; i++) {
   temp = objNum[numKeys[i]];
   sum += temp;
}
console.log(`sum is ${sum}`);
ready
string literal key
let temp;
let sum = 0;
for (let i=0; i < 10000; i++) {
	switch (i%4) {
		case 0:
			temp = objStr["key_0"];
			break;
		case 1:
			temp = objStr["key_1"];
			break;	
		case 2:
			temp = objStr["key_2"];
			break;	
		case 3:
			temp = objStr["key_3"];
			break;						
	}
   
   sum += temp;
}
console.log(`sum is ${sum}`);
ready
numeric literal key
let temp;
let sum = 0;
for (let i=0; i < 10000; i++) {
	switch (i%4) {
		case 0:
			temp = objNum[0];
			break;
		case 1:
			temp = objNum[1];
			break;	
		case 2:
			temp = objNum[2];
			break;	
		case 3:
			temp = objNum[3];
			break;						
	}
   
   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.