In Lua, should a variable be defined every iteration of the loop, or before the loop?

In particular, in Lua, I can harm you:

for i = 1, 10 do local foo = bar() -- do stuff with foo end 

instead of this:

 local foo for i = 1, 10 do foo = bar() -- do stuff with foo end 

I mean, will Lua try to allocate new memory for foo each iteration? Can the first block lead to slower execution?

+4
source share
1 answer

Go for the safest alternative, which is to use the smallest area for all variables. In terms of efficiency, local variables are stored on the stack; no memory allocation is allocated inside the loop.

+5
source

Source: https://habr.com/ru/post/1333704/


All Articles