Pairs vs ipairs vs Nothing? Which is better?

What are the differences between these 3 lines of code? Especially what is the difference between ipairs and pairs?

for i, Objects in ipairs(workspace:GetChildren()) do
   print(Objects)
end
for i, Objects in pairs(workspace:GetChildren()) do
   print(Objects)
end
for i, Objects in workspace:GetChildren() do
   print(Objects)
end
3 Likes

Pairs and “nothing” function identically from what I know. ipairs is better for tables with numeral indices (arrays) as it is more efficient but only works with said arrays.

In this case ipairs would be the best choice since GetChildren returns an array.

So there is no noticeable difference between them, right?

1 Like

in should be used now, is more performant than both pairs/ipairs. I performed a test to see which one manages to do more iterations, and in was the way to go. The difference is not so big but considering in iterates in order for arrays (table), then it’s a good choice to stop using pairs/ipairs. You can use ipairs when you want the iteration to stop once it meets a nil a value in the array (table), which in my opinion is not desired most of the time.


(pairs)
(ipairs)
(in)

9 Likes

I see you already got the solution, but for the most part yes.

FWIW: the proper name for the third option is generalised iteration.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.