Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.46
Chrome 118
Windows
Desktop
one year ago
Test name Executions per second
for..of Map.entries() 5451.7 Ops/sec
Map.foreach 5630.8 Ops/sec
for..of Map.entries() no destructuring 6563.1 Ops/sec
Script Preparation code:
AخA
 
var array = Array.from({ length: 10000 }, () => {
  const n = Math.floor(Math.random() * 100)
  if (n > 26) {
    return n
  } else {
    return String.fromCharCode('a'.charCodeAt(0) + n);
  }
});
var obj = new Map();
for (let i = 0; i < array.length; i++) {
  obj.set(i, array[i]);
}
Tests:
  • for..of Map.entries()

     
    for (const [key, val] of obj.entries()) {
      const foo = val + 'a';
    }
  • Map.foreach

     
    obj.forEach((val) => {
      const foo = val + 'a';
    })
  • for..of Map.entries() no destructuring

     
    for (const entry of obj.entries()) {
      const key = entry[0];
      const val = entry[1];
      const foo = val + 'a';
    }