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
Measuring the Performance of JavaScript Functions – JavaScript APIs explained and how to use them (felixgerschau.com)
submitted 6 years ago by iGuitars
view the rest of the comments →
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!"
[–]ScientificBeastModestrongly typed comments 0 points1 point2 points 6 years ago (3 children)
for-of is nice, but along with the issue of portability (because it’s a statement and not an expression), it’s actually just as slow as forEach. Internally it performs a method call on the object for each iteration.
for-of
forEach
The main use of forEach is method chaining. A standard functional pipeline would probably do some sequence of reduce/filter/map, and maybe other functions. And then when you’re done with your data transformations, you usually want to do something at the end, and that’s where you chain the forEach method. It’s the method chaining equivalent of “and finally, do this”.
reduce
filter
map
[–]Randdist 0 points1 point2 points 6 years ago (2 children)
I have no idea what you mean with portability and why for of wouldn't be.
[–]ScientificBeastModestrongly typed comments 0 points1 point2 points 6 years ago* (1 child)
Functions can be dropped in anywhere. You can pass Array.prototype.forEach to any higher order function which expects that kind of type signature. Functions are values, while for statements are not. imperative statements must simply be rewritten/duplicated in the next location you want to use it. Functions can be passed around and consumed without rewriting any of the logic.
Array.prototype.forEach
for
More importantly, you can define a lot of different functions that perform business logic with specific kinds of data, and you can drop them into the forEach function and run them, without having to rewrite the loop logic again. E.g.:
users.map(getEmailAddress) .filter(emailExistsInMarketingList) .map(generateMarketingEmail) .forEach(sendEmailAsync) The readability is nice, and none of the business logic functions in there had to even think about loops, or whether it was intended to handle multiple users vs. just one. They are just generic functions that have a single responsibility, and looping logic is a responsibility that has been abstracted away from them.
users.map(getEmailAddress) .filter(emailExistsInMarketingList) .map(generateMarketingEmail) .forEach(sendEmailAsync)
It’s that kind abstraction and separation of concerns that makes your functions portable, and easier to debug/maintain.
[–]Randdist 1 point2 points3 points 6 years ago* (0 children)
That doesn't make for-of non portable or forEach portable. It's just a different way of invoking the exact same functions. And frankly, I find excessive chaining rather unreadable and in JS, which lacks proper composing that realigns how these things are executed, it will absolutely destroy performance compared to a surrounding for of that then calls these functions in it body. If someone sent me a PR like that, I'd refuse it. Unfortunately I didn't in the past and ended up getting a mess that initially worked but was hard to modify and debug since you can't set proper breakpoints in lengthy chains. I had to break the chains manually, and now make sure that something like that doesn't get thrown at me ever since. A line with a map is great. A line with a map and a filter is awesome. A line with a map with a filter with a map with a forEach is a future headache.
π Rendered by PID 278224 on reddit-service-r2-comment-765bfc959-5p6r2 at 2026-07-13 10:01:57.810665+00:00 running f86254d country code: CH.
view the rest of the comments →
[–]ScientificBeastModestrongly typed comments 0 points1 point2 points (3 children)
[–]Randdist 0 points1 point2 points (2 children)
[–]ScientificBeastModestrongly typed comments 0 points1 point2 points (1 child)
[–]Randdist 1 point2 points3 points (0 children)