top of page

Fastest Loop in JavaScript?

Updated: Jun 2, 2023

In programming, loops are used to repeat a block of code. For example, if you want to show a message 100 times, then you can use a loop. It's just a simple example; you can achieve much more with loops


JavaScript Loop

Which 'forloop' is faster?

The answer is for (reverse)

When comparing the forward and reverse for loops, they take almost the same amount of time to execute. There is only a slight difference of 0.1ms because the for (reverse) loop calculates the starting variable let i = arr.length only once, whereas the forward for loop checks the condition i < arr.length after each variable increment. However, this minute difference is negligible and can be ignored.


On the other hand, the forEach method is a part of the array prototype. When compared to a normal for loop, both forEach and for...of loops tend to take more time for array iteration. This is because they have additional overhead and processing involved in executing the iteration logic.


Types of Loops in JavaScript


1. For Loop:

The for loop is a commonly used loop in JavaScript. It has the following syntax:

for (initialExpression; condition; updateExpression) {
    // for loop body
}

In the for loop, the initialExpression initializes and/or declares variables and is executed only once. The condition is evaluated, and if it is true, the block of code inside the for loop is executed. After each iteration, the updateExpression updates the value of the initial expression. The condition is evaluated again, and this process continues until the condition becomes false.


Example:

const fruits = ["Apple", "Mango", "Orange", "Grapes"];
let text = "";
for (let i = 0; i < fruits.length; i++) {
  text += fruits[i] + "<br>";
}

2. forEach:

The forEach method is available on arrays and allows you to iterate over each element of an array. It has the following syntax:

array.forEach(function(currentValue, index, arr) {
    // forEach loop body
});

In the forEach loop, a callback function is executed for each element of the array. The currentValue parameter represents the value of the current element, the index parameter (optional) represents the index of the current element, and the arr parameter (optional) represents the array itself.


Example:

const fruits = ["apple", "orange", "Mango"];
fruits.forEach(function(item, index) {
  // forEach loop body
});

3. For...of:

The for...of loop is used to iterate over iterable objects such as arrays, strings, etc. It has the following syntax:

for (let x of iterable) {
    // for...of loop body
}

In the for...of loop, the x variable (required) takes the value of each element in the iterable object for each iteration, and the loop body is executed.


Example:

let text1 = "JavaScript";
let text2 = "";

for (let x of text1) {
  text2 += x + " ";
}

4. For...in:

The for...in loop is used to iterate over the properties of an object. It has the following syntax:

for (let x in object) {
    // for...in loop body
}

In the for...in loop, the x variable (required) represents a property name for each iteration, and the loop body is executed.


Example:

const person = {fname:"Rahul", lname:"sharma", age:27};
let text = "";

for (let x in person) {
  text += person[x] + " ";
}


Conclusion:

  • The for loop is the fastest but may be less readable.

  • The forEach loop provides better control over the iteration process.

  • The for...of loop is slower but has a cleaner syntax.

  • The for...in loop is slower and less commonly used.

Choose the loop type based on your specific requirements, considering factors such as performance, readability, and the type of data you are iterating over.

0 comments

Recent Posts

See All
bottom of page