Open In App
Related Articles

What is the fastest way to loop through an array in JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we will see the fastest way to loop through an array in JavaScript. JavaScript has a large variety of loops available for performing iterations. The following loops along with their syntax are supported by JavaScript.

for loop: A for loop comprises a variable initialization, a condition for variable evaluation, and an expression to determine the termination of the loop. It performs a fixed number of iterations, therefore it must be used in scenarios where the number of times the loop iterates is known prior.
In the case of constants, we have the following kind of syntax:

// for loop that performs 10k iterations
for (let i = 0; i < 10000; i++) {
    // Statements 
}

In the case of arrays:

// Loop that runs up till the length
// of the array 
for (i = 0; i < array.length; i++) {
    // Statements 
}

while loop: A while loop performs initialization outside, before the beginning of the loop, then a condition is validated and then the expression is evaluated. The execution of the while loop continues until the condition is satisfied.

While loop in case of arrays, follow the syntax:

// Initialization 
let x = 0;


// Condition checking
while (x <= arr.length+1) {
    // Increment
    x += 1;
}

.forEach() loop: Each element of the array is subjected to a function performing some operation or task. The function is specified as a parameter of the forEach method in JavaScript. It performs highly efficient tasks where functional code is required. It is basically a callback function dependent highly on the operation performed inside.

// Evaluation is performed element
// wise of the array 
let arr = []                      
arr.forEach((ele) => { 
    // Statements
})                              

for…of Loop: This is a new version of for loop. This loop requires assigning a temporary name to each element of the array. It runs extremely fast in the case of small data sets. It is much more readable in comparison to the traditional for loop. It performs an iteration over JavaScript’s in-built iterate objects like String, Map, Set, and Array.

for (let val of arr) {
    // Statements
}

Comparison of performance of loops available in JavaScript : 

  • In case of multiple iterations of the loop, and where the size of the array is too large, for loop is preferred as the fastest method of elements iteration.
  • While loops perform efficient scaling in the case of large arrays.
  • In the case of functional codes, for each loop performs better with a much optimized time.
  • In case of smaller iterations and smaller data sets or operations, which are used occasionally, forEach( ) performs with greater speed.
  • An optimized version of for loop to simulate length caching, i.e. to store the array length to prevent its computation repetitively.

The for loop with a length caching alternative is mostly, therefore, the preference. 


Last Updated : 16 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads