I’ve seen in a script the author redeclared native functions used in his code with the comment of “optimization”.
local tonumber = tonumber
local CreateThread = Citizen.CreateThread
local Wait = Citizen.Wait
local TriggerEvent = TriggerEvent
Does this actually offer any noticable optimization to the resource?
1 Like
Yeah, from what I’ve read accessing local variables is faster than accessing global variables. The only thing I’m not sure is if tonumber is global or local, 'cause if it’s already local then why the script author made id local again lol
These are typically micro-optimizations.
This puts the function which are in the global scope into the local scope which saves on function calls.
When a resource needs to access some specific variable MANY times, it becomes efficient to store that variable in local memory over having to access the global memory space, and in the case of CreateThread and Wait, having to check the Citizen global memory space for that function.
tonumber is a global which does a table lookup on _ENV, defining it to a local saves you from this lookup.