1

For readability's sake, I often find myself declaring new variables for data I already have in hand, and was wondering, does this have any major impact in performance?

Example of what I do:

const isAdult = this.data.person.age >= 18;
const hasChildren = this.data.person.hasChildren;

if (isAdult && hasChildren) {}

Instead of doing:

if(this.data.person.age >= 18 && this.data.person.hasChildren) {}
Lucas_Nt
  • 116
  • 1
  • 7
  • 3
    _"does this have any major impact in performance?"_ - No. – Alon Eitan Feb 11 '20 at 11:13
  • 2
    I wouldn't worry about that at all. The performance you'll gain (if you gain any) would be negligible and would come at the cost of degrading readability and maintenance which would definitely cripple your team performance to deliver. Whoever says differently is definitely optimising the wrong thing IMHO. – customcommander Feb 11 '20 at 11:15
  • 2
    Pretty much mandatory reading at this point: [this article by Eric Lippert](https://ericlippert.com/2012/12/17/performance-rant/) – VLAZ Feb 11 '20 at 11:24
  • 2
    The `if` statement is a lot more readable if written with `isAdult` and `hasChildren`, first example is definitely what you should go for when your `if` statements become hard to read. I do this myself. – goto Feb 11 '20 at 11:25
  • 1
    Are you *truly*, *really*, and *actually* experiencing a performance bottleneck because you've defined two variables? If not (and I very much doubt you do), then don't even think about that. – VLAZ Feb 11 '20 at 11:25
  • I'd be more concerned about making sure that your variables are [scoped correctly](https://stackoverflow.com/a/500459/11228357) so that they get picked up by [garbage collectors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management) – volt Feb 11 '20 at 11:47

2 Answers2

0

Generally the answer to the question "Will it have a performance impact?" is "what did you find when you tested it?".

Yes of course it will have a performance impact - but in order to have a noticeable, or even measurable impact, you will need to declare a lot of variables.

Generally, it is good practice to minimize the number of variables (without re-using them for different purposes) in order to make it easier for human beings to understand the code, in the case of Javascript it is sometimes a good idea to create more direct references to data to data to avoid the cost of long scope chains - but this is starting to go off-topic.

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • "Yes of course it will have a performance impact" - [Citation Needed]... Why do you think this will have an impact? These types of variable declarations will be inlined by compiler. – Kyle Pittman Feb 11 '20 at 17:49
  • You want a citation to prove that a computer takes more time to process many instructions than it takes to process a few instructions? – symcbean Feb 17 '22 at 19:24
-1

Short answer: no, it does not make any major impact on performance.

sweapper
  • 11
  • 4
  • 1
    OP asked about javascript, not Java. – symcbean Feb 11 '20 at 11:28
  • @symcbean in fairness, I don't expect multiple declarations to matter in pretty much any programming language. Nowadays all code gets optimised either at compile time or even at execution time. Inlining is a very easy optimisation path and I doubt extra bindings really matter that much anyway.\ – VLAZ Feb 11 '20 at 11:35
  • Eh? That statement makes no sense at all. While better compilers will allocate different hashing sizes for maps based on size estimates, this ultimately has very little impact on the dereferencing cost. – symcbean Feb 11 '20 at 11:39