Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

301
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!