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

140
Views
Which is more efficient in a for loop? Assigning reused value to a var or using the value directly many times?

I was refactoring my code today to make it more efficient and suddenly a question popped into my mind. Which is more efficient and which is the best practice that can be used in any language or framework?

Approach-1

 for(let i=0;i<testList;i++){
      let reusedVar = testList[i];
        let testData= {
            firstName: reusedVar.firstName,
            lastName: reusedVar.lastName,
            ....
       }
     this.testTableData.push(testData);
     this.dataSource = new MatTableDataSource(this.testTableData);
 }

Approach-2

 for(let i=0;i<testList;i++){
  let testData= {
       firstName: testList[i].firstName,
       lastName: testList[i].lastName,
       .....
   }
 this.testTableData.push(testData);
 this.dataSource = new MatTableDataSource(this.testTableData);
}

Which approach from above is more efficient and why?

Thanks in advance.

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!