Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Which is more efficient using multiple elseifs or multiple if statements?

Asked by 4 years ago
local Mystery = {}


local function PrintWhatType1(X)
    if type(X) == 'number' then
        print('number')
    elseif type(X) == 'table' then
        print('Table')
    elseif type(X) == 'string' then
        print('String')
    end
end



local function PrintWhatType2(X)
    if type(X) == 'number' then
        print('number')
    end

    if type(X) == 'table' then
        print('table')
    end

    if type(X) == 'string' then
        print('String')
    end
end

What is the difference and what function is more efficient to do?


PrintWhatType1(Mystery) PrintWhatType2(Mystery)
1
The two do the same thing, but I think there's no difference between them. I would say "elseif", because it's less writing. TheRealPotatoChips 793 — 4y
0
such performance differences, if they existed, would fall into the category of micro optimizations, as the time it takes to swap from one to the other is more than the performance boost you actually get from the optimization over thousands of users. theking48989987 2147 — 4y
0
there is a spacial benefit to using the former however, as the latter incorporates an extra newline character. however, looking at it from the perspective of raw character count , if including the newline characters, the difference is that the latter is one character shorter than the former theking48989987 2147 — 4y
0
Elseif is shorter and more efficient and simpler. By the way, use typeof instead of type. iuclds 720 — 4y

Answer this question