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

311
Vistas
JS : In while + nested for loop, is it better to re-instantiate or to reset value?

I'm not familiar with the js ecosystem and came across the following questioning: In a while loop, I do this: I have a for loop which pushes elements into an array. At the end of the for loop, I do something with the array. Rinse and repeat.

Is it better, performance wise to reinstantiate my array (example1) at the beginning of my while loop, or is it better to reset its value (example2)? Why?

Assume while and for both loop a huge number of time.

Example1:

while(condition) {
  const myArray=[];
  for(int i=0;i<X;i++){
    myArray.push(i);
  }
  doStuff(myArray);
}

Example2:

let myArray=[];
while(condition) {
  for(int i=0;i<X;i++){
    myArray.push(i);
  }
  doStuff(myArray);
  myArray=[]
}

I'm starting to dig the subject but it feel like this is the kind of question where 1) experts may have a quick and precise answer 2) other newcomers may be interested in... So here I am.

about 4 years ago · Juan Pablo Isaza
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