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

What's better: a standard "for" loop or a "in pairs" loop?

Asked by
Uglypoe 557 Donator Moderation Voter
8 years ago

Just want to know to optimize some of my scripts.

1 answer

Log in to vote
1
Answered by 8 years ago

They both are good. Numeric for loops are slightly faster because they don't use function calls. Pairs uses function calls. Pairs is essentially this:

function pairs(x)
    return next,x,nil
end

So when the loop runs, it keeps calling that function thus making it slightly slower. HOWEVER you won't notice much of a difference unless a table has millions of elements inside of it.

Ad

Answer this question