Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

89
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.

7 months ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos