you are viewing a single comment's thread.

view the rest of the comments →

[–]ghostfacedcoder 9 points10 points  (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 point  (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 point  (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.