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

Is there any advantage of using in pairs instead of next?

Asked by 5 years ago

Basically, can I just use for i,v in next, instead of for i,v in pairs() in every for loop, since next is faster? Or is there a scenario where I should use pairs/ipairs

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

next is faster than pairs, but only by a tiny amount. Don't use next instead of pairs for the purpose of optimizing; use what makes the most sense to you. The reason next is slightly faster than pairs is because pairs is just a call to next. This is what pairs really looks like:

function pairs(t)
    return next, t
end

Think of it this way: The atomic weight of an atom doesn't account for the mass of it's electrons; that number is just too small to mean anything. Likewise, programmers should not sacrifice more explicit code when the difference in performance is also too small to mean anything.

0
So what is the point of pairs.... TiredMelon 405 — 5y
1
Readability. For some people pairs is more readable, so that's what they can use. Trying to optimise your code by changing things like pairs is generally a bad idea, because it's almost always not the real bottleneck of your code. fredfishy 833 — 5y
Ad

Answer this question