I have seen people talk about ipairs
and pairs
being deprecated sooner or later, and I have also seen the following widely used lately:
for index, value in table do end
What’s the difference? Is using no iterator function better?
I have seen people talk about ipairs
and pairs
being deprecated sooner or later, and I have also seen the following widely used lately:
for index, value in table do end
What’s the difference? Is using no iterator function better?
From what I know pairs
and ipairs
aren’t deprecated, I always use them in for
loops, I’ll look in the Documentation
Edit: Couldn’t find anything in the Documentation about them being deprecated, there’s still Tutorials about them
pairs
is used for Dictionaries, while ipairs
is used for Array’s, hence the i
in ipairs
which stands for index
in table
as far as im concerned is the regular way of doing it with a for
loop, pairs
and ipairs
just sort of modify it.
As mentioned in my reply, it’s a "shadow “change”, a fairly recent one, I believe it works as a combination for both ipairs/pairs, we don’t know much about this change as the only thing we know that pairs/ipairs aren’t needed anymore, more information will probably be covered in the next luau recap.
Use pairs
when iterating through dictionaries.
Use ipairs
when iterating through arrays.
Use neither when you specify a custom __iter
metamethod. Here’s the announcement for this new feature:
ipairs
is faster than pairs
when iterating through arrays. Use ipairs
when you can. Using neither is fine as well. Using ipairs
and pairs
is great if you want to know what you’re iterating over at a glance.
I just tested this out in the command bar, it seems that it iterates over arrays in order like ipairs
, and dictionaries just like pairs
would. Seems to be for simplicity and overall QOL.
It’s a change to prevent confusion between ipairs/pairs as doing none “automatically” choose the right one for you as you mentioned in your test.
They aren’t deprecated yet, however they are still widely supported and is still being used to-date.
It’s important for developers to understand how the functions work, and when to use them, even if they are eventually deprecated in the future.
pairs()
Iterates over tables, Returns key-value pairs (does not maintain the key order)
ipairs()
Iterates over arrays, Returns index-value pairs (maintains the key order)
Thanks for the quick explanation! I didn’t know what the difference was till now, thanks
Hey there!
It’s no worries! You are very much welcome, feel free to read over the documentation if you have any questions.
If my reply helped you, feel free to mark my post as the solution!
Alright, I just did some benchmarking and got interesting results: (Nanoseconds, 1000 calls per contestant)
Test 1:
ipairs: 1223120699.9858842
pairs: 1229931900.0449033
None: 1199392499.984242
Test 2:
ipairs: 1250860099.9135523
pairs: 1269632699.8993754
None: 1222484700.0325098
Test 3:
ipairs: 1211694299.9418826
pairs: 1226462899.9400883
None: 1257150299.9635413
It looks like generalized iteration is the winner, excluding test 3 where ipairs
wins. For the majority, it looks like ipairs is the best option due to the performance not changing to rapidly, but using generalized iteration shouldn’t hurt.
I have a question, could you tell us which one is faster directly? I’m too lazy right now to check which one is faster and which one is slower
Generalized Iteration doesn’t “choose” because it uses one generic mechanism that traverses keys like pairs and guarantees order with indices.
Remember, the point of generalized iteration is that it allows engineers to be able to optimize its functionality for our use-cases without breaking compatibility. The iterator functions will probably never be deprecated but again they’re not really being looked into anymore either.
Ah got it, I tried to search for the recap link but I got a 502 error.
Alright, thanks for telling me about this post, but from what I read, generalized iteration is basically just ipairs
and pairs
, but you don’t have to guarantee that it’s an array, and uses the same algorithm for dictionaries and arrays?
I pressed the link but it didn’t give me any error, strange…
Btw ipairs works on dictionaries if you order your keys in numerical order like [1],[2],[3]. However in most cases that’s not going to work because dictionaries usually don’t have ordered keys
*Correction, ipairs()
only works with numerically indexed table (i.e. arrays), not with dictionaries or tables with non-numeric keys. It’ll return the index and the corresponding value of each element in the array, in order from 1 to the last numerically indexed element.
On the other hand, pairs()
iterates over all key-value pairs of a table, regardless of whether the keys are numeric or not, and returns them in an arbitrary order (not necessarily the order in which they were added to the table).
To iterate over a dictionary with ordered keys, you can use the table.sort()
function to sort the keys numerically and then use pairs()
to iterate over the table. However, this approach can be inefficient for large tables and may not be necessary in many cases.
That’s what an array is. A dicitionary is a table with keys that aren’t numbers, and if you write something such as: {"info", true, 1}
, it will be converted into an array during runtime.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.