3 ms·
If it was written like while true { let hello = "world"; } The scope would keep getting freed then re-allocated, which will be slower than simply cont
by judge2020 6y ago
If it was written like
while true {
let hello = "world";
}
The scope would keep getting freed then re-allocated, which will be slower than simply continuously setting an existing variable's memory location over and over again.
- ihuman 6y agoIf that was the reason, then shouldn't "const" be as slow as "let"?
- kjksf 6y agoIt should be valid to move `const` outside of the loop, since by definition it cannot be changed. `let` can be changed so cannot be optimized out this way (at least not without doing more complex analysis and proving that the variable is not being changed).
- charliesome 6y agoWhile the creation and destruction of a scope every loop iteration is the correct mental model for what this code does, there's no reason that implementations must actually perform any allocations - especially for an example like this where it is trivially determinable that no bindings escape the immediate scope.
- chrismorgan 6y agoThat would apply to a pure interpreter, but all JS runtimes will do at least some optimisations, even on their baseline profile.
- cosmosgenius 6y agolet wasn't inside the loop, which is what made it weird.