Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda