Recently I wrote a benchmark to test efficiency of variable storing methods including:
-local variable
-table[“variable”]
-module[“variable”]
in a module script.
And it turns out that local variables outperforms the other two by over 3 times in time consumption (not sure about memory though) every single time.
Table variables beats module storing ones by a little only, probably the difference of metatable lookups.
I currently have a ModuleScript attribute system that allows me to edit module attributes outside by a controller.
The thing is there may be multiple attributes, and sometimes a large amount of them,
doing :GetAttributeChangedSignal:Connect(function(AttributeName)...end) with if statements and local variables does allow me to simplify it by a bit and get a slight performance increase, but I still prefer doing something like
the performance difference between a table lookup and a variable is not going to matter for you, they both take sub-microseconds. use whatever works best and don’t use tables for everything, putting stuff in a table doesn’t always make things more organized.
I know that. It’s just in this case that the variables are referenced to many times every frame, which might cause performance issues over time. Or is it a better way not to use RenderStepped for handling smooth camera tilts, for example?
How did you do the benchmark? If it takes, say, 1 million table indexes for this issue to appear, then you should only care about it if your code indexes the table 1 million times in your use case. Otherwise, what’s the point? You’re spending time trying to optimize a tiny, insignificant cost out of your code instead of spending time working on the other parts of the project, and in the end it might even make your life more difficult because of it.