I am trying to improve the performance of my bottleneck functions that (ab)use the OnUpdate event. Since this event is called so frequently, I would like to optimize the functions.
My idea is to move my temporary variables outside of the function.
Here's a silly example:
Code:
myFrame:SetScript("OnUpdate",function(self,elapsedTime)
for i=1,#array do
local name = UnitName("raid"..i.."target")
-- do important stuff
end
end)
The concept is that moving the temporary variables on the outer scope of the function will still encapsulate the variables, but would recycle the existing variables when the function gets called again. This should reduce the garbage collection.
The fix would be:
Code:
do
local i, name
myFrame:SetScript("OnUpdate",function(self,elapsedTime)
for i=1,#array do
name = UnitName("raid"..i.."target")
-- do important stuff
end
end)
end
Does this extended variable scope have any benefit, or should I be letting the garbage collector pick up every local variable? Do for-loop variables override preexisting variables and build new localized ones?
Thanks to any Lua-gurus out there!