Define once in module, or many times in each script

I’m wondering what the most optimal way to define a bunch of variables is.
(this is probably obvious, still curious though)

I currently use a bunch of module scripts, with one “main” script connecting all of them. I’m wondering if I need to define something, say the Player, each and every time. And whether that would be un-optimal.

local info = {
   Character = game.Players.LocalPlayer
   Camera = workspace.CurrentCamera
   ...
}
return info

Accessing this module above for each script.

pros/cons that I think may occur
pros - one defining
cons - accessing a huge table probably isn’t great, and likely is more laggy
OR
Just defining everything regularly, for every script.
pros - easy, no big table
cons - need to define a ton of the same thing, over and over.

Sorry if this is a basic/no sense question, just want some clarity.

1 Like

How would you define a ‘huge’ table?

I believe using a modulescript adds more customizability and organizes your code. In my opinion, you shouldn’t worry about using tables (it all depends on your use-case).

1 Like

Nope, dictionary lookups in Lua are constant time, meaning the size of the table/dictionary has no bearing on how long it takes to look up a key. If you’re interested in the CS behind why that is, read up on hash tables specifically / associative arrays in general.

Although repeatedly looking something up in a table is still slower than looking up a local variable. For critical paths (code in tight loops that run often), declaring local variables to make lookup faster is a real optimization technique.

3 Likes

Thank you Downrest and Emma, I’ll be marking this as a solution :+1:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.