Code run speed: function in loop?

Would my code be inefficient, redeclaring the local function every time if instead of

local function A() ... end

while wait() do
    A()
end

I do

while wait() do
    local function A() ... end
    A()
end

It’s not end of the world inefficient, but it’s both bad practice and it incurs a small performance overhead to re-declare functions that could otherwise be declared only once.

5 Likes

See for yourself: run print(A) in both cases, the later you’ll see the memory addresses can change, think about the work you just created to: find that new address, discard the function when it’s no longer used.

1 Like

Just as another note, if you are going to loop every wait(), or use a while loop in general, there’s no point really calling a function:

while wait() do
   ... -- Function code
end

Unless of course you use the function in other sections in your code.