• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

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

almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error