Shtille's blog A development blog

Map enumeration performance comparison

We need compare two methods of map enumeration:

  • via forEach method
  • via entries enumeration

Code setup will be:

const N = 100000;
var m = new Map();
for (var i = 0; i < 10; ++i)
	m.set(i,i);

The first case:

var x = 0;
m.forEach(function(value){
	x += value;
});

The second case:

var x = 0;
for (const [key,value] of m.entries()) {
	x += value;
}

According to benchmark site the second one is 58.62% slower. This is kinda strange.