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

163
Visualizações
Are const values re-evaluated in maps and filters

Does JavaScript treat:

array.filter(x => {
  const test = [1, 2, 3]
  return test.includes(x);
});

the same as:

const test = [1, 2, 3]
array.filter(x => {
  return test.includes(x);
});

or will the const cause extra work on each evaluation in the former?

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

0

With the first, you're declaring an array only once.

With the second, you're declaring an array every time. Memory has to be allocated for each array and then garbage collected.

The first is more efficient.

Note that const does not mean the value is constant (except with primitives), const means that the variable reference can't change (for example, you can call .push on test on the inside array, even with const, but you can't reassign test)

However it is possible that your engine could optimize for this

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