Steven D'Aprano
6/7/2015 3:56:00 PM
On Mon, 8 Jun 2015 01:13 am, Joey@still_Learning.invalid wrote:
> I have an extensive script that often generates the same global
> variable in multiple functions. I declare each a global by defining as
> window.variable = xxx.
>
> Do multiple declarations require greater compiling or otherwise
> overhead, i.e., performance degradation?
Surely this is premature optimization? Unless the calculation of xxx is
horribly expensive, or you are performing it millions of times, I find it
hard to believe that this could be anything more than a micro-optimization
(shaving a few hundredth of a millisecond off the runtime or so).
> Instead of re-declaring should I declare 'var xxx' outside the
> functions? Does this accomplish the same as above?
It seems to me that an outer declaration should be preferred, not because it
is "more efficient to run", but because it is more readable, more
understandable, and more easily maintained. If you decide to change it from
variable = xxx to variable = yyy, you only need to change one place,
instead of twenty.
--
Steven