small variable question

hey guys, so I see variables like this all of the time



local Color = Color


Why do people create these variables? I mean, isn’t it pointless?
I’m just a little confused, I’d appreciate any help I can get.

If you’re seeing something like
[LUA]local table = table or {}[/LUA]
it’s for autorefresh reasons.

Basically when a lua file reloads all variables reset, unless you do the above which tells lua to set the value to what it was before the refresh, or set it to another value (an empty table in this case) if it’s the first time running the script and the previous value is nil.

I’m not seeing things like that, I know what that’s for. I’m specifically seeing things like



local Color = Color


More specifically, variables that equate to functions or libraries.
ex.2



local draw = draw
local surface = surface
ect.


Optimization, but I feel like if you don’t really write the most efficient code then localizing will be useless

Weird, is there anything that describes why it’s optimal to localize everything like that, and what the difference between localizing it all and leaving it be, performance-wise?

Local variables are faster to find compared to global variables.

Little to nothing, unless you’re calling something thousands of times in a single execution.

People also pair it with the “module” function, which requires you to localise all of the global functions you’re going to use with the module since it sets the environment.

Hmm, interesting… What is the module function?

I see… Well, the thing is, I’ve seen this a lot. Why do people use it so often if it has such little impact?

module allows you to load a file with

require, although, it’s somewhat limited in GMod and there are much better alternatives. You can see it in action here: https://github.com/Facepunch/garrysmod/blob/master/garrysmod/lua/includes/modules/hook.lua

[editline]3rd August 2017[/editline]

And many people just put the functions they use in the file locally at the top even if the impact is small since it’s a “quick and easy” optimisation. It’s really not needed unless you’re using the functions often.

I’m relatively new to Lua, so this is really interesting. I really appreciate it!