Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
Fastest way to loop through Map keys

Let's say I have a map with the keys:

const map = new Map([['a', 'Apple'], ['b', 'Banana']])

I want to loop through the keys of the map ('a', 'b').

I know these ways of looping through the keys:

Map.prototype.forEach()

map.forEach((_value, key) => {
    console.log(key)
})

Pros: Simple

Cons: Have to ignore first param in function - (_value, key) => instead of just key => .

Create array from Map.prototype.keys() and then Array.prototype.forEach()

[...map.keys()].forEach(key => {
    console.log(key)
})

Pros: Callback can just be key =>

Cons: [...map.keys()]. Might be confusing when reading the code and might be less performant.

for of loop through Map.prototype[Symbol.iterator]()

for (const [key] of map) console.log(key)

Pros: Probably fast. Easy to read.

Cons: Not an arrow function (opinion that forEach is better).

for of loop through Map.prototype.keys()

for (const key of map.keys()) console.log(key)

Very similar to previous method.

So I want to know which way is the most performant.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I created a JSBench with the methods. These are the results from fastest to slowest:

  1. Map.prototype.forEach()
  2. Create array from Map.prototype.keys() and then Array.prototype.forEach()
  3. for of loop through Map.prototype.keys()
  4. for of loop through Map.prototype[Symbol.iterator]()
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda