I will get a reasonable assumption and say that the temporary dead zone is the culprit.
This cycle, which, apparently, refers to your micro-enterprise, was eaten by the optimizer for breakfast , since Vyacheslav Egorov likes to talk in his conversations. And even this is not so, and the engine will increase the variable a million times, it will work the same in both functions.
What is the difference between a . In your first fragment, this is at the beginning of the function, there is nothing before it. There is no temporary dead zone; it is essentially a function-region variable; changing it to var would not have affected (try). Therefore, when a function is called, a region with a variable is created and the value is initialized to 0 , then some code is executed (or not).
On the contrary, in the second fragment there is a temporary dead zone. In the code preceding the let declaration, access to a should throw an exception. Therefore, when a function is called, an area is created and the slot for a reserved, but remains uninitialized. In this state, the code is launched (or not), and only after that the variable will be initialized and assigned the value 0 .
So, if let is in the middle of the code (or after it), the scope is more complex. This can lead to the fact that the optimizer will handle it differently, maybe even affect the variable i , which is in the same scope, or, perhaps, it will not be able to perform certain optimizations at all.
Bergi source share