Storing vector3.new or data types in a variable is faster than calling it every time?

I heard somewhere on devforum that this:

local vector3new = Vector3.new

for i = 1, 10000 do
	local newVector = vector3new(1, 1, 1)
end

Is faster than this:

for i = 1, 10000 do
	local newVector = Vector3.new(1, 1, 1)
end

I want to know if this is true and if it is true, is it worth it for me to do this for smaller loopings?

I’ve heard the contrary actually, either way the performance difference should be minimal. If anything, just doing

local new = Vector3.new(1, 1, 1)

for i= 1, 10000 do
    local newVector = new
end

is probably the fastest. I don’t think this is what you meant tho

1 Like

Once upon a time, saving it in a variable would have been faster for two reasons. First, accessing local variables is faster than accessing global variables. Vector3 is a global variable. Second, by doing A.B you would be performing an extra operation vs a simple A.
To my knowledge, local variables are still faster. But it looks like Roblox added a system for these old built-in globals to become a special type that is more optimized. By doing that, they also fixed the A.B business.
Running a test, you’ll find they’re basically the same. No difference whatsoever.

3 Likes

not worth it, just makes the script needlessly longer in my opinion

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