What is more optimized?

I know this is a simple question, just trying to make my code a little more optimized!
By the way I will be calling the “Attack()” function a lot, so my question is making variables for it be better in the long run?

Thanks, Alpha.

1:

local Table = {
	["Damage1"] = 10,
	["Damage2"] = 20,
}

Attack(Table.Damage1)

2:

local Table = {
	["Damage1"] = 10,
	["Damage2"] = 20,
}

local Damage1 = Table.Damage1
local Damage2 = Table.Damage2

Attack(Damage1)

The second one would technically be more optimized for repeat access but it does not matter in the long run. For you to have any noticeable difference in performance between the two, it would take millions or more iterations.

It’s also not viable to write out a local variable for each element in the table as it grows. There’s also the issue of the variables caching the values and not passing current numbers to the function if the value ever changes.

tl;dr dont waste your time with caching everything to a variable

The second option is the most optimal because the indexing operation is not performed when calling the function.

However, table indexing is one of the most optimal things that lua has, so the gain is probably negligible.

1 Like