Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Performance

Reduce Activity in Loops

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.

Bad:

for (let i = 0; i < arr.length; i++) {

Better Code:

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.