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

309
Visualizações
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 à 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