Re:Using const/let instead of var can make JavaScript code r...
by judge2020 on Wednesday October 21, 2020 @01:22AM
(#24844971)
If it was written like
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.
[ Parent
| on HN ]
Re:Using const/let instead of var can make JavaScript code r...
by charliesome on Wednesday October 21, 2020 @02:00AM
(#24845143)
While 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.
Re:Using const/let instead of var can make JavaScript code r...
by chrismorgan on Wednesday October 21, 2020 @02:24AM
(#24845245)
That would apply to a pure interpreter, but all JS runtimes will do at least some optimisations, even on their baseline profile.
Re:Using const/let instead of var can make JavaScript code r...
by ihuman on Wednesday October 21, 2020 @01:37AM
(#24845042)
If that was the reason, then shouldn't "const" be as slow as "let"?
Re:Using const/let instead of var can make JavaScript code r...
by kjksf on Wednesday October 21, 2020 @02:08AM
(#24845181)
It 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).
Re:Using const/let instead of var can make JavaScript code r...
by cosmosgenius on Wednesday October 21, 2020 @07:39PM
(#24853499)
let wasn't inside the loop, which is what made it weird.