Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

2 comments

0

So what is the point of pairs....

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.