Related Articles

Related Articles

What is the fastest way to loop through an array in JavaScript ?
  • Last Updated : 05 Nov, 2020

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 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 in known in prior.
In 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 case of arrays:

// Loop that runs up til 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 loop, then a condition is validated and then the expression is evaluated.The execution of while loop continues until the condition is satisfied.
While loop in case of arrays, follow the syntax as:

// Intialization 
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 of the element of the array. It runs extremely fast in case of small data set. 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 : 

  1. In case of multiple iterations of the loop, and where the size of array is too large, for loop is the preference as the fastest method of elements’ iteration.
  2. While loops perform efficient scaling in case of large arrays.
  3. In case of functional codes, for each loop performs better with a much optimized time.
  4. In case of smaller iterations and smaller data sets or operations, which are used occasionally, forEach( ) performs with greater speed.
  5. An optimized version of for loop is 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. 

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :