Which is faster? for, for..of and forEach methods in JavaScript | JavaScript Interview Guide

preview_player
Показать описание
for, for..of and forEach
There are different ways to loop over arrays in JavaScript, but it can be difficult choosing the right one.There is a classic JavaScript for loop, JavaScript forEach method and the for-of loop, introduced in the sixth edition of EcmaScript (ES6), allows the programmer to loop over the actual iterable objects. In this video You will learn different ways to loop or iterate JavaScript arrays.

for loop

The most basic type of iteration method in JavaScript is the for loop. It takes three expressions; a variable declaration, an expression to be evaluated before each iteration, and an expression to be evaluated at the end of each iteration.  Javascript for loop is fastest, but ugly on the eyes.

for...of loops are  the fastest when it comes to small data sets, but they scale poorly for large data sets. slowest, but its a syntactic sugar over for loops.

The execution time of .forEach() is dramatically affected by what happens inside each iteration. fast and designed for functional code

The results were quite interesting and not what I was expecting.  The execution of javascript really depends on
various factors like type of operating system like windows and the type of browser like chrome, IE,Firefox

The traditional for loop is the fastest, so you should always use that right? Not so fast - performance is not the only thing that matters. It is rare that you will ever need to loop over 1 million items in a frontend JS app. Code Readability is usually more important, so default to the style that fits your application. If you prefer to write functional code, then forEach is ideal, while for-of is great otherwise. Fewer lines of code means shorter development times and less maintenance overhead - optimize for developer-happiness first, then performance later.
Рекомендации по теме
Комментарии
Автор

You should've used `let` instead of `var` for the oldschool for loop. In my benchmark in Node, it made the code almost 50 times faster. Here the comment at the end of each option shows the median time of a thousand runs in ms:

let opt=['for(let i=0;i<a.length;i++)a[i]', // 1.32
'for(let i=0, l=a.length;i<l;i++)a[i]', // 1.37
'a.forEach(v=>{v})', // 1.46
'for(let v of a)v', // 4.76
'for(var i2=0, l=a.length;i2<l;i2++)a[i2]'] // 61.42

let a=Array(1e5).fill().map((_, i)=>i)

for(let o of opt){let

anth
Автор

Every time someone use "var" a little cat dies

lluisjg
Автор

A bit late on this comment, but this is misleading. The thing being measured is the time it takes to create a string, then concat all the intermediate results of "adding" a number to that string over an over to produce the string "01234567891011...49999". This is creating a new string on each loop, copying the memory from the old string to a new string, and getting longer with each successive loop. Roughly O(n^2), stressing the garbage collector in really oddball ways, and has almost nothing to do with the performance of the for loop.

If you convert the string to number 0 and rerun, you should be able to add numbers this way (not concat but sum) at the rate of about a billion per second on a second rate laptop.

jnsh