Loops are commonly used in programming.
Each statement within a loop, including the for
statement, is executed during every iteration.
Placing statements or assignments outside the loop, when possible, can improve the loop’s performance.
for (let i = 0; i < arr.length; i++) { |
let l = arr.length; for (let i = 0; i < l; i++) { |
The inefficient code accesses the array’s length property on each iteration of the loop.
The optimized code accesses the length property outside the loop, improving its performance.