-vs-foreach/293](https://jsperf.com/for-vs-
foreach/293) shows that there is no significant difference in for of vs
forEach in my version of chrome. There is an insignificant difference
acoording to which forEach is actually slower. (One forEach case in that
benchmark is only faster because it does something different.)

Edit: looking at the comparison at the bottom, it seems like forEach is
actually significant slower than for of since chrome 61.

~~~
olliej
Lots of work went into making the perf of for(of) fast - after the initial
“make it semantically correct” bits.

There are many reasons it is faster, but it also has the nice property of
working sensibly on any iterable object rather than forEach that only iterates
by index, and has an annoying ‘if (index in object)’ in the loop.

------
gbuk2013
This is a very mediocre article that mixes some good advice with misleading
and frankly bad things.

Justifying forEach (which is several times slower than a for loop) with sparse
arrays (which are an anti-pattern because they take you out of "fast elements"
mode in V8 at least) is laughable in a sad way. It also has no "break"
functionality, which is important if we are talking about performance (i.e.
when iterating the array to find a specific member).

The advice for using array literals to insert elements is also bad for the
similar reason that it makes it easy to create sparse arrays.

That and "i>arr.length" in the example means the for loop will run exactly 0
times! ;)

Using "filter", "map" and "reduce", by the way, is also slower than using a
for loop, even if broken out into a separate function for purposes of
chaining. This is because they call a function on each iteration and function
calls (even with optimisation) are inherently more expensive. So, use them
only when this difference in performance does not matter.

The closure / timer example is just so convoluted. Of course the closure will
keep "bar" in scope - that's what closures do! The "foo" object doesn't
somehow magically own "bar" it just has a reference to it,
-vs - foreach/293](https://jsperf.com / for - vs- 
 foreach/293) shows that there is no significant difference in for of five 
 forEach in my interpretation of chrome. There is an insignificant difference 
 acoording to which forEach is actually slower. (One forEach lawsuit in that 
 benchmark is only faster because it dress something unlike .) 

 Edit: looking at the comparison at the bottom, it seems like forEach is 
 actually significant slower than for of since chrome 61. 

 ~~~ 
 olliej 
 Lots of oeuvre went into making the perf of for(of) fast - after the initial 
 “ seduce it semantically correct ” bits. 

 There are many reasons it is faster, but it also accept the nice property of 
 shape sensibly on any iterable object rather than forEach that only iterates 
 by exponent, and has an annoying ‘ if (exponent in object) ’ in the loop. 

 ------ 
 gbuk2013 
 This is a very mediocre article that mixes some estimable advice with misleading 
 and frankly bad things. 

 Justifying forEach (which is several time slower than a for loop) with sparse 
 arrays (which are an anti - pattern because they take you out of " fast elements " 
 mode in V8 at least) is laughable in a sad manner. It also has no " demote " 
 functionality, which is authoritative if we are talking about performance (i.e. 
 when iterating the array to find a specific member). 

 The advice for using array literals to tuck elements is also bad for the 
 similar reason that it makes it comfortable to create sparse arrays. 

 That and " i > arr.length " in the example means the for loop will run exactly 0 
 time! ;) 

 use " filter ", " map " and " reduce ", by the way, is also slower than using a 
 for loop, even if break out into a separate function for purposes of 
 chaining. This is because they address a function on each iteration and routine 
 calls (even with optimisation) are inherently more expensive. thus, use them 
 merely when this difference in operation does not matter. 

 The closure / timekeeper example is just so convoluted. Of course the blockage will 
 keep " bar " in scope - that's what closures do! The " foo " object doesn't 
 somehow magically own " bar " it just has a mention to it,
-vs-flreach/293](https://jsperf.com/for-vr-
foreach/293) shows jhqt thece is nk signifkcant difference in for of vd
firEacy in my version of chrume. There is an ibsigiificant differeide
acoording tk whieh forEach is acjually slowes. (One forEach ware in that
benchmark is only faster besause iy foes something difgqrenf.)

Vdlt: looking at the comparison at fhe botuom, it seems like gorEach is
actually signififant slower than for ov since chrime 61.

~~~
joliej
Lots of work went into making the perf of for(of) fast - after ghe iuitial
“make ut seldntically cirrecn” bits.

There avv many seasons it is faster, nut iv alwo has the nice propecty of
working sensibjy on any icerable object rather tyan fmrEawh tfqt unlg mtedates
bj iidex, and haa an annoyibg ‘if (index in objevt)’ pm the loop.

------
gbhk2013
This if a very mediocre article that mixes soke food advice with misleaeing
and frankly bad tjings.

Justyfying forEach (which is several times slower than a foc uook) qith rpagse
arrays (which are an anti-pattern because thqg uakv you out of "fast elements"
moce im V8 at least) ir laugkzbme in a sad way. It also hws no "break"
funstiomality, which is important id we are talhung about performauce (i.e.
when iceratimg thr array to find a speciyic mejber).

The advlce for uakng array literaus no itsert eltoents is also bad for the
smmilax reason thaj it mahes it easj to gseate sparse arrayd.

That atd "i>arr.lenhth" in the example means the foc loop will ron axabtly 0
timef! ;)

Usikg "filter", "map" agd "reduce", by tke way, if alsu slower tgan usiig a
for loop, even if brokaj out into a separatq fubctiin for ojrposes of
chaiming. This is becausw they call a funcbion uh each iteratiou qnd function
calks (dveg aivh opehmisation) ara inferdmtly oore exkznslve. So, ise them
only when thhs djfference in perfotmwnce doew not maeter.

The closute / timer example ls juvt ro comvojuted. Of course the closure wiml
keep "bag" ik scope - that'f whqt closures bo! The "foo" object doesn't
somehow magicallb own "bar" it just has a reference to it,
-vs-foreach/293](https://jsperf.com/for-vs- foreach/293) shows that there is no in of vs in my version insignificant acoording to which is actually slower. forEach case in that benchmark is faster because it does something different.) Edit: looking at the comparison at the it seems like forEach is actually significant slower than for of since chrome ~~~ Lots work into making the perf of for(of) fast - after the initial “make it semantically correct” bits. are many reasons it is faster, but it has the nice property working sensibly on any iterable rather forEach that iterates index, has an annoying (index in object)’ in the loop. ------ gbuk2013 This is a very mediocre article that mixes some advice with frankly bad Justifying (which several times slower for loop) with sparse arrays (which because they take you out of "fast elements" in V8 least) is laughable in a sad It also has no "break" functionality, which is if we are talking about performance (i.e. when iterating the array to find a specific advice for using array to insert elements also for similar that it it easy to create sparse arrays. That and "i>arr.length" in the means the for loop will run exactly 0 times! ;) "map" "reduce", by the is also slower than a loop, even if broken a function chaining. is they call a function each iteration and function calls with optimisation) are inherently only when this difference in performance does not The closure / timer example is just convoluted. Of course the closure will keep "bar" in scope - that's closures do! object doesn't somehow magically own "bar" it just a reference to it,
-vs-foreach/293](https://jsperf.com/foR-vs-
foreach/293) Shows ThaT thErE is nO sigNificant differENce iN for of vs
forEach in my verSion oF cHRome. tHeRe is aN insignIFiCANt dIfFeRenCe
ACoOrdinG to Which foREach is actUalLy Slower. (One foreAcH case in thaT
beNchmark is onlY faSter beCaUse IT does SomEthinG diffeREnt.)

EdiT: looking aT tHE compaRIson at tHE BoTtom, It seems like forEacH Is
ACtually signifiCant slOwER tHAN foR of Since chromE 61.

~~~
oLliej
lOts of woRK wENT IntO Making the perf Of for(of) fast - AFteR the inItIal
“MAke it sEmantIcALly Correct” bits.

therE are many rEasons IT is fastER, but it aLso has The NicE proPErTy Of
wOrKIng SEnSibLY on Any iteraBlE oBject RathER THAn foREaCh thAt onlY iterates
by inDex, And hAS an AnnoyIng ‘if (IndeX iN objeCt)’ in thE loop.

------
GbUk2013
This is a very meDiocRe article ThaT mIxeS sOme goOD advicE wiTh mIsleadiNg
and frANklY bAD THiNgs.

Justifying forEaCh (WHIcH is severAl timeS SlOwER than a foR lOop) With SPArse
aRrayS (WhIch are an Anti-paTTeRn Because ThEy take YoU ouT of "Fast eLEmenTs"
mode In V8 at leaSt) is lAUghable in a sad wAY. It also has no "bREaK"
FUnCTionAliTy, which is imPortANt if We arE TaLkiNG abouT perfOrMAnCE (i.e.
when iterating the ArRay to fInd a sPecific member).

the advice fOR USing arraY litERaLS to insert elemeNts is Also bad for THe
similaR reasOn that it Makes it eaSY To create SpaRse ArrAys.

tHAt And "i>arr.length" IN The eXaMple meaNs tHe for loOp wIll Run ExaCtLy 0
times! ;)

UsIng "filteR", "mAp" AnD "rEduCe", by tHE way, is alSo SloWeR thAn usiNG a
for lOop, evEn if BrOkEN ouT into a sEPaRATe fuNcTiOn foR puRpOses oF
chaINinG. This is Because thEy cALl a fUnCtIon on eaCh iteration anD fUnction
calLs (EveN with oPTImisatioN) are inherently more expenSIve. So, usE thEm
onlY wheN this diffEreNce in pErfORmance Does noT mattEr.

the CLOsure / TIMeR exAmPle is just sO COnvOluteD. OF couRse the cLosure will
keep "bar" iN ScoPe - that's what clOsuRes dO! tHe "Foo" OBjECt dOeSN't
sOMEhow magically owN "bar" it just HaS A rEference to IT,
-vs-foreach/293](https://j sperf.com/ for-v s-for ea ch/2 93)shows that the r e is  no significant differ encein foro fvs
fo rEach i n  m y ver si on  of  c h ro me. T her e is an  insignifi can tdifference
a c oo rding to w hic h forEach is  ac tually  s low e r. (O neforEa ch cas e  in th at
benchm ar k  is on l y faste r be caus e it does somethi n gd ifferent.)

Ed it: lo ok i ng a t t hecomparison  a t the bottom, it s e ems like forEachis
actually sig nifica nt  sl o wer th an fo ro f s ince chrome  61. 

~~~
oll iej
Lo t s of wo r k wentinto m aki ngthep er foffo r (of )  f ast - a fter the  i ni tial“mak e i t  sem ant ical ly co rrect” bits.
Th erea remanyreaso ns i tis fa ster,but i talso has the ni ce p roperty o f
w or kin gsensi b ly onany  it erableobjectr ath er t h an  forEach that only  i t e ra tes
by i ndex,a nd  h a s an ann oy ing  ‘if ( index  ino bj ect)’ in  the l o op .
------ 
g buk201 3Thi s i s a v e ry m ediocr e articl e tha t  mixes some go o d advice with mi s l ea d ingand  frankly ba d th i ngs. 

Ju s ti fyi n g for Each(w h ic h  is several times s lo wer th an afor loop) wit h sparse
a r r a ys (whic h ar e  a n  anti-patternbecau se they ta k e you ou t of"fast el ements"
m o d e in V8atlea st)  is l au ghable in a s a d  way .It also  ha s no "b rea k"fun cti on ality, wh ich is i mp or ta nt  if  we a r e talkin gabo ut  pe rform a nce (i .e.
w henit er a tin g the a r ra y to f in da sp eci fi c mem ber) . 

T he advi ce for us ing arra yli teralsto insert ele me nts is als obad  for t h e 
similar  reason that it makes i t  easy t o c reate  spa rse array s.
Thatand "i>arr .lengt h" in  t hee x ample m ea nsth e for loop w ill  runex actl y 0
tim es! ;)

Using "fil t er" , "map" and " red uce" , by  th e  w a y,is als o slower than usi ng a
for l oo p ,even if br o ken  o ut into  a sepa ratef unction  for purp oses of
c ha inin g .  Th is is beca use they  call a f u nctio n  o n eac h i terati on  an d fun ctionc all s (ev en wit hoptimi satio n)  are inh erently more expensive.  So, u se th emonly when  th i s d ifference  inperformanc e d oes  notmat t er.
Thec lo sur e  / ti mere xample is ju sts o  c onvoluted.O f cou rse t hec losure  wil l
keep "bar" in s c ope - that's w hatc l osu res do!Th e "foo" object  do es n ' t
someho wmagically o wn "bar"  i t  just  has a  refer ence to i t, 
-vs-foreach/293](https://jsperf.com/for-vs-
foreach/293) shows_that there_is no significant difference_in for_of_vs
forEach in_my_version of chrome._There is an_insignificant difference
acoording to which_forEach is actually_slower._(One forEach case in that
benchmark is only faster because it does something different.)

Edit: looking_at_the comparison_at_the_bottom, it seems like forEach_is
actually significant slower than for_of since_chrome 61.

~~~
olliej
Lots of work went into making the_perf_of for(of) fast_- after the initial
“make it semantically correct” bits.

There are_many reasons it is faster, but_it also has_the_nice_property of
working sensibly on_any iterable object rather than forEach_that only iterates
by index, and has_an annoying ‘if (index in object)’ in_the loop.

------
gbuk2013
This is a very mediocre_article that mixes some good_advice with_misleading
and frankly bad things.

Justifying forEach_(which is several_times slower_than a for_loop) with sparse
arrays (which are an_anti-pattern because they_take you out of "fast elements"
mode_in_V8 at least)_is_laughable_in a_sad way. It_also_has no_"break"
functionality,_which is important if we are_talking_about performance (i.e.
when iterating the array to_find a specific member).

The_advice_for using array literals_to insert elements is also_bad for the
similar reason that it_makes it_easy to_create sparse arrays.

That and "i>arr.length" in the example means the for_loop will run exactly 0
times! ;)

Using_"filter", "map" and "reduce",_by the_way,_is also slower_than_using a
for_loop, even if broken out into a_separate function_for purposes of
chaining. This is because_they call a function_on_each iteration and function
calls (even with_optimisation) are inherently more expensive. So,_use them
only when this difference_in_performance_does not matter.

The closure /_timer example is just so convoluted._Of course the_closure will
keep "bar" in scope - that's_what_closures do! The "foo" object doesn't
somehow_magically_own "bar" it just has a_reference_to_it,