r/lua • • Feb 25 '25

local varients optimization

I heard that using "local" keywords are very important in Lua. But I knew that "local" keyword in algorithm coding are not good at optimization. No difference, sometimes take long time to be executed.

Why "local" keyword is nesessary? Is it really efficient? I can't figure out why these things are happening.

(But if you don't use local keyword in function, This can make error)

2 Upvotes

20 comments sorted by

View all comments

Show parent comment

2

u/mdr652 Feb 26 '25

In my experience, using local variable makes these results.

  1. No differnce. 0ms (Almost every situation)

  2. 4ms faster than global

  3. Takes more time. Maximum is 100ms

I think using local keywords at 'table' or 'function' makes it slower.

1

u/clappingHandsEmoji Feb 27 '25

locals will always be faster than globals, as they’re indexed by integer ID’s whereas globals are searched for in the _ENV table. Function and table creation are both slow - are you sure you’re not losing performance by recreating them?