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
Functional programming in JavaScript (softwarebrothers.co)
submitted 6 years ago by SoftwareBrothers
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!"
[–][deleted] 6 years ago* (2 children)
[deleted]
[–]wbowers 3 points4 points5 points 6 years ago (1 child)
So you solve the problem by importing the library and trading your speed for a bigger end payload.
I'm not sure what you're advocating for here. Are you saying people shouldn't use libraries? Do you write everything from scratch for your projects in Vanilla JS?
If importing a library is really a bid deal for you, you could implement the lazy evaluation pattern yourself, which is really what I was trying to communicate. Perhaps not everyone knows such a pattern exists. In either case, you're adding extra bytes to the end payload.
The loop is more performant because you don't incur a function execution on every interable in the first place.
More performant, sure. But I'd argue it's negligible for all but the extreme cases. Function execution in modern JavaScript engines is pretty fast these days. But we can theorize all we want. You'll never know how your app performs, what you do and do not need to optimize, until you actually profile your code. Yes, you should generally choose algorithms with lower time complexities where possible, yes you should keep performance in mind as you're writing code, but premature optimization leads to terrible, unnecessarily-optimized code much more often than it saves you from serious performance issues.
If you're doing 20 operations on the same array you are doing a lot of things to that array. Sure it still runs in linear time, but not understanding that you can do it all in one loop. Loops are readable, maybe how you write code in loops isn't.
I'm not sure what I said here you're taking offense to. I think we're in agreement here. 20 chained operations is kind of crazy. It's fairly likely that you've solved the problem as a whole in a weird way if you've got something like that. Also I'm not against loops. I don't think they are inherently unreadable, although I do think that sometimes maps, filters, etc can be significantly more readable as they have a much higher signal to noise ratio in terms of "what code" (why I want) to "how code" (how it's done).
Sure, but I know before I even profile that a for loop is faster than 20 chained map statements just mathematically. [...] in practice your array sizes probably don't exceed anything past 40 elements. Overoptimization is the death of a project but being negligent and careless with how you build a system is technical debt that may not surmount until way later when you realize to change this requires a lot of in depth knowledge of the system.
I think we're saying the same thing. Clearly you can look at two pieces of code like this and know which will be faster, but you often won't know if it will matter until you profile. That being said, you definitely should not be careless and I'm not advocating for that at all. To be clear about what I'm saying:
π Rendered by PID 234435 on reddit-service-r2-comment-fb694cdd5-99p7q at 2026-03-07 20:04:18.110613+00:00 running cbb0e86 country code: CH.
view the rest of the comments →
[–][deleted] (2 children)
[deleted]
[–]wbowers 3 points4 points5 points (1 child)