Should I be localizing these variables/functions?

For example:

local workspace = workspace
local sin = math.sin
local cos = math.cos
local tan = math.tan

is this more efficient in terms of performance? Why?

2 Likes

No. With the optimizations Roblox has made via Luau, these are even more redundant. Not only redundant, but counterproductive. To make your code do faster, it should do less, not make each step 1% faster. That is not how you should do it.

3 Likes

You shouldn’t do that, that will make this ‘slower’ then faster

This is not more efficient in terms of performance. You are making the code slower, and consuming more ram usage (Atleast theoretically).
Variables are stored in the ram, for quick access. You are re-defining defined functions, which are not at all efficient.
Also, why:

local workspace = workspace

Makes no sense.

Also, I am not able to explain me correctly. Feel free to correct me if I’m wrong.

I’m unsure about the ram thing though.

Also: @sjr04

Yes it does. workspace is normally a global variable, and localizing it will save time whenever you have to access it. Instead of indexing the environment where globals are, the local variable will already be easily accessible in memory.

This is definitely a microoptimization, but there is a noticeable difference. Here’s how much faster local variables are after 10,000,000 accesses:
image
… and here’s the code I used to test this:

local workspace2 = workspace

wait(5)

local t1 = tick()

for i = 1, 1e7 do
	local x = workspace
end

local t2 = tick()

for i = 1, 1e7 do
	local x = workspace2
end

local t3 = tick()

print("local:", t3 - t2, "\nglobal:", t2 - t1)

(For anyone who thinks I just cherry picked the best results, you can try it yourself. I got consistently faster speeds with locals even when I flipped the two loops around.)

5 Likes

Good to know that. Does this also apply to the math functions that TwiistedRoyalty mentioned in the thread? Also, thanks for teaching me something new today. One doubt; how can you give the local variable the same name as the global name? That’s what I meant by “this makes no sense”.

It just counts as overriding it, you can do this with any variable.

It applies even more so to these. For the math functions, you have to fetch the math table from the global environment and then grab sin (or whichever function) from that table. Indexing the environment and a table makes it doubly* (not really double but whatever) slow.

It’s still a microoptimization, though. Notice how I had to do it ten million times for there to be a significant difference.

4 Likes

How can it override? Like he localled workspace, right? If it was:

workspace = workspace

then it would be considered as overwriting.
Does localling do overwriting?

1 Like

Localizing it will overwrite the variable, yeah. You can do this with anything. For example:

local real_print = print
local wait = 1

local function print(...)
	return real_print(script:GetFullName(), ...)
end

print(wait) --> wherever.Script, 1
3 Likes