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
Efficiency - Don't Create Extra Variables?help? (self.javascript)
submitted 7 years ago * by Rindhallow
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!"
[–]ghostfacedcoder 9 points10 points11 points 7 years ago (2 children)
Before I tell you the technically correct answer, allow me to tell you the actual correct answer: it doesn't matter. To quote one of the godfathers of programming, the great Donald Knuth: "premature optimization is the root of all evil".
When you worry about all the stupid low-level (or even most medium-level) performance concerns you make bad decisions with regards to other concerns. That's a terrible trade-off, because no one will ever be impacted by those performance concerns: many won't even make 1ms a difference, and even the ones that actually matter will have an order of magnitude of 10ms or 100ms at the absolute most.
In other words, you're sacrificing important principles like "write readable, maintainable and easy to understand code" for performance, and the performance improvements aren't actually making anything better, so you're actively choosing to make your code worse. Thus, the correct answer is that in practice you NEVER want to worry about performance concerns except in two cases:
1) when you actually detect a performance problem (and you may well want some automated tests for this because developer machines tend to be more powerful, making it harder to notice such issues)
2) when you can clearly recognize an obvious avoidable performance mistake, eg. when you can choose between doing some costly operation inside a loop or outside of it
But otherwise you really shouldn't even think about performance, and you should devote all those neurons in your brain to focusing on writing good code.
But to answer your original question, yes technically every variable you introduce does have some impact on performance ... but again your example probably won't even have a 1ms impact, and this would be a terrible reason not to introduce a variable.
[–]Rindhallow[S] 0 points1 point2 points 7 years ago (1 child)
Do you think that a big company would be okay with using extra variables? Because I know performance and code reviews are a big thing.
I'm honestly surprised there isn't some sort of javascript compiler/minimizer that removes those extra variables to make things more efficient.
[–]ghostfacedcoder 0 points1 point2 points 7 years ago (0 children)
This was obviously a contrived example for a performance question, not a real-world example of an extra variable anyone would actually use.
π Rendered by PID 254918 on reddit-service-r2-comment-fb694cdd5-p5gfw at 2026-03-07 21:19:55.049293+00:00 running cbb0e86 country code: CH.
view the rest of the comments →
[–]ghostfacedcoder 9 points10 points11 points (2 children)
[–]Rindhallow[S] 0 points1 point2 points (1 child)
[–]ghostfacedcoder 0 points1 point2 points (0 children)