What is the difference between localising arguments or using them raw in a function?

Raw usage of arguments:

local function ABTest(A, B)
    print(A, B)
end

Localising arguments:

local function ABTest(A, B)
    local A = A
    local B = B

    print(A, B)
end

I really don’t see the difference in either, though I have seen developers and their code opt to use the latter over the former. Is there some kind of performance or functional difference that I’m unaware of, or is this a matter of preference?

I notice that in a similar but different context, developers also assign methods of standard libraries into upvalues at the top of their scripts. Such an example is as shown below:

local MATH_ABS = math.abs
local MATH_CLAMP = math.clamp
local CFrame_new = CFrame.new
2 Likes
local function ABTest(A, B)
    print(A, B)
end

…and…

local function ABTest(A, B)
    local A = A
    local B = B

    print(A, B)
end

…are the exact same. There is no optimization and any developer using the latter most likely does not understand that. Use the former since it’s more readable.

It’s a micro-optimization that saves microseconds, not enough to worry about. Avoid it if it makes the code less readable.

8 Likes

Did you saw that from me?

Anyways the reason why I did that was because it’s used very often such as in a function binded to RenderStepped used for visual effects, it’s called about 60 per second and I want it to be as fast, efficient and smooth as possible, but you can use it for other situations too, this does improve efficiency but don’t over do it.

https://devforum.roblox.com/t/the-basics-of-basic-optimization/26756

https://devforum.roblox.com/t/why-i-dont-sleep-micro-macro-optimizing-mega-guide/71543

https://devforum.roblox.com/t/the-art-of-micro-optimizing/226753

Always prioritize Readability first, and performance second.

Where I saw it doesn’t matter - that’s irrelevant to the point of the thread.

For sure. Readability is something I’m very big on.

Indexing functions is not expensive at all. Those save microseconds, it’s not noticable at all.

1 Like