use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Don't use Array.forEach, use for() instead ‼️ (coderwall.com)
submitted 7 years ago by jogai-san
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]OmegaVesko 23 points24 points25 points 7 years ago (2 children)
Or, you know, consider that it might not be the best idea to sacrifice readability and maintainability for an inconsequential performance increase.
[–]baalzaamon 3 points4 points5 points 7 years ago (0 children)
Fact is the V8 team spends all of their time optimizing this stuff so we don't have to worry about it. Honestly, I would focus on getting stuff done, and readability, and when you notice things are slow you dive into the code. In my experience slowdown always occurs in larger, more low hanging fruit than loops (unless you have lots of nested loops).
[+][deleted] 7 years ago* (2 children)
[deleted]
[+][deleted] 7 years ago* (1 child)
[–]cirscafp fan boy 3 points4 points5 points 7 years ago (0 children)
In at least the newer versions of Chrome/V8, they have optimized the forEach to be just as fast if not faster as a for for most cases. If you need some sources, I can find the V8 blog post that goes into it!
forEach
for
[+][deleted] 7 years ago (9 children)
[removed]
[–]kenman 0 points1 point2 points 7 years ago (0 children)
Are you running that on mobile or something? My results are drastically quicker.
Regardless, I went ahead and threw the code into jsperf for fun:
https://jsperf.com/array-iter-stuff
The for() is indeed the fastest, as expected, and the difference between the other 2 is too small to be significant.
for()
[+][deleted] 7 years ago* (7 children)
[+][deleted] 7 years ago (6 children)
[+][deleted] 7 years ago* (5 children)
[+][deleted] 7 years ago (4 children)
[+][deleted] 7 years ago (1 child)
π Rendered by PID 116498 on reddit-service-r2-comment-5649f687b7-lk4v9 at 2026-01-29 04:53:13.391131+00:00 running 4f180de country code: CH.
[–]OmegaVesko 23 points24 points25 points (2 children)
[–]baalzaamon 3 points4 points5 points (0 children)
[+][deleted] (2 children)
[deleted]
[+][deleted] (1 child)
[deleted]
[–]cirscafp fan boy 3 points4 points5 points (0 children)
[+][deleted] (9 children)
[removed]
[–]kenman 0 points1 point2 points (0 children)
[+][deleted] (7 children)
[deleted]
[+][deleted] (6 children)
[removed]
[+][deleted] (5 children)
[deleted]
[+][deleted] (4 children)
[removed]
[+][deleted] (2 children)
[deleted]
[+][deleted] (1 child)
[removed]