Which case is more optimized || faster: if-then-loop-else-loop-end or loop-if-then-else-end

I’d like to know which option is faster:

-- have two loops for the two cases
local function f(table, argument)
    -- we assume that `argument` can cause two different behaviors

    if (argument) then
        for index, value in ipairs(table) do
            ...
        end
    else
        for index, value in ipairs(table) do
            ...
        end
    end
end

-- or:

-- have a single loop checking the case
local function f(table, argument)
    -- we assume that `argument` can cause two different behaviors

    for index, value in ipairs(table) do
        if (argument) then
            ...
        else
            ...
        end
    end
end

Which would be faster? If not well-explained, please ask.

the top would be faster because it won’t be constantly spamming if (but only just barely). But I honestly wouldn’t worry about it and use the bottom one because its cleaner and you won’t copy paste code

Note that if statements take a fraction of a fraction of a fraction of a millisecond to complete.

The bottom one would be better because you 1. won’t copy paste code and 2. don’t have to search thousands of lines to find the one line you need to edit and then edit them in all the other if statements

1 Like

Guess I’ll take that answer.
:man_shrugging: