Тёмный

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

Code Pro
Подписаться 2,7 тыс.
Просмотров 6 тыс.
50% 1

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.

Опубликовано:

 

26 июн 2020

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 11   
@Andrewstown
@Andrewstown Год назад
very good to know!
@anth1338
@anth1338 Год назад
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
@yuvakishore1748
@yuvakishore1748 3 года назад
Super
@Zerowolrd
@Zerowolrd 3 года назад
Thank you :)
@CodePro-Jayanth
@CodePro-Jayanth 3 года назад
Pls subscribe to my channel, if you like the video
@paramarajv7047
@paramarajv7047 4 года назад
Sooo amazing tq
@CodePro-Jayanth
@CodePro-Jayanth 4 года назад
Thank you too
@paramarajv7047
@paramarajv7047 4 года назад
@@CodePro-Jayanth hey ...i eagerly waited for your channel update🙄
@CodePro-Jayanth
@CodePro-Jayanth 4 года назад
@@paramarajv7047 very soon I will post
@lluisjg
@lluisjg 7 месяцев назад
Every time someone use "var" a little cat dies
@user-cq9fq4vm4b
@user-cq9fq4vm4b 9 месяцев назад
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.
Далее
СМОТРИМ YOUTUBE В МАЙНКРАФТЕ
00:34
Просмотров 799 тыс.
Javascript Arrays Made Simple: Iterators (Part 1)
11:55
FOR LOOP vs. forEACH vs. FOR...OF | Learn JavaScript
13:13
How Slow Is JavaScript? | Prime Reacts
15:34
Просмотров 176 тыс.
The purest coding style, where bugs are near impossible
10:25
forEach() vs. map() - What’s the Difference?
4:30
JavaScript Loops Made Easy
10:52
Просмотров 167 тыс.
Iterable vs Enumerable in JavaScript
9:15
Просмотров 20 тыс.
Is JavaScript Spread a Performance Killer? Quick Fix
9:34