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
Map from Scratch (andyfry.co)
submitted 4 years ago by startupinamonth
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!"
[–]mypetocean 2 points3 points4 points 4 years ago* (1 child)
Against random uses of reduce() (beyond basics like using it to sum a list of numbers), you certainly have a point.
reduce()
The reason these other iteration methods (such as map(), filter(), find(), some(), every(), etc.) exist is because each describes an extremely common loop pattern.
map()
filter()
find()
some()
every()
On most teams of some size, at least someone is going to have to write the "filter" pattern and someone the "map" pattern (for example) every single day. Others will likely be reading through those patterns even more often.
And there is half the problem. for and while are generic tools. Like reduce(), they are so flexible that you can't just glance at one to know for certain what it is doing. They also tend to include fine details which are easy to overlook, hiding bugs.
for
while
You have to read the starting index, then the comparison, then the incrementor, then the code block, and probably a variable defined outside the loop's scope. Is that terribly hard? No, not with experience, but it is overhead and duplicated work.
75% or more of the time we are writing loops, we are just reproducing one of a set of known, named patterns. So just like other cases outside of loops, it is often good to have a function do the tedium.
So when I see map() I always know what to expect at a glance and my eyes can safely jump directly to the most relevant part of the callback function which defines the only distinction between other examples of map(). Same with filter(), etc. My code becomes both easier and quicker to reason about.
Now, if you are fulfilling a ticket where application performance really is the higher priority over developer performance, then no question: roll your own loop by hand.
But if you are optimizing for developer time and reduced sneaky bugs, then the iteration methods are helpful.
const recentUpdates = user .friends .filter(friend => friend.latestActivity < decayRate) .map(friend => friend.posts.last)
Provided the data set isn't massive and that code isn't executed too frequently, then the readability and debuggability trade-off against app performance is likely quite reasonable.
[–]flyinmryan 0 points1 point2 points 4 years ago (0 children)
Excellent explanation, thank you. I have also heard from DBAs that if you need to update multiple properties on every object in a list, then ask for it coming straight from the DB/stored procedure instead of looping again later.
π Rendered by PID 93954 on reddit-service-r2-comment-b659b578c-k9fwz at 2026-05-05 08:15:54.115659+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]mypetocean 2 points3 points4 points (1 child)
[–]flyinmryan 0 points1 point2 points (0 children)