For example, would this:
while (true) {
var random = Math.random();
}
... be less efficient than the following, in most implementations?
var random;
while (true) {
random = Math.random();
}
Thanks for your input.
Edit: In case it wasn't obvious, I'm mostly worried about lots of repeated (de)allocations occurring in this example.
JavaScript does not have block scoping.
In the first example, the var text
declaration is hoisted out of the while
block. In both cases, the variable is declared only once. In both cases, the variable is assigned a value once per iteration of the while
loop.
var
function
-scopedNo, variables are initiated upon entry into the scope, so random
exists before the var statement is even reached.
JavaScript doesn't have block scope, and random
's declaration would be hoisted to the top of its scope anyway (variable object).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With