Why do i need pairs or ipairs?

I mean, why do i need pairs if i can write
“for i, v in table” without anything else?

3 Likes

I’m pretty sure ipairs() is faster, even if marginally

Using nothing is faster than both theres a post debunking this

1 Like

do you mind if i see this post in question

ipairs loops through the table in numerical order. Also if Ipairs encounters a nil value in the table it will stop looping through it.

This solution can also explain alot

1 Like

Yeah, just do not use those because luau automatically does it now

ipairs is used to loop through arrays, pairs is used to loop through arrays and dictionaries

while nothing is uhhhh, nothing!

There’s not really much of a use for pairs now. ipairs is used for number-index tables, like the person above me said, but pairs will iterate over any and all keys in a table. pairs is slower than ipairs and other iterators like next. Using either no iterator or next is faster, I’d recommend next because you also have the option to start part-way through the table.

2 Likes

Considering all this and the fact that the pairs is a little difficult to understand, I think I should use “nothing”.

1 Like

If you use the “nothing” syntax but (for some reason) want to change how the iteration runs, you can change the __iter() metamethod of a table and set it as the metatable. Just thought I’d put that out there.

2 Likes

Did Roblox add this or is it in standard Lua?(I am talking about “nothing” method)

1 Like

Standard Lua only allows pairs, ipairs, or the next iterator syntax. Other syntax errors in standard Lua. I guess Luau added this method but still kept the pairs iterator inherited from Lua 5.1 :man_shrugging: .

Edit: You can use the “nothing” syntax in standard Lua if you modify the table’s __iter() metamethod to return an iterator. This is because it provides an iterator function to the iteration, which is required.

1 Like

Ipairs is not faster there was a post about this in the devforum I think Ipairs has a speed of like 5 seconds while no pairs which has been recently updated by roblox like this year or last has a speed of like 3 seconds there’s a test on this but can’t seem to find the post.

it’s quite simple:

for i, v in table do
print(v)
end
1 Like

if they took 3 and 5 seconds to iterate that would be worrying. I tested it and it took about 0.002 for ipairs and something like 0.05 for pairs.

Sorry my mistake yuh this timers are correct for both pairs and ipairs but no pairs is faster and more efficient.

I’m pretty sure pairs is not faster, I don’t see why it would be anyway. pairs was the slowest when I tested, with ipairs being second slowest, and next and no iterator being the fastest.

Not true, if the table is an array both ipairs and pairs will loop through in order.

The only time iterating through pairs is unordered is if it’s a dictionairy, and you wouldn’t be able to use ipairs on the dictionairy.

My bad thought it did, but ipairs does stop when reaching a nil value and it will not continue

It’s a common misconception, I used to think that too when I was newer to scripting. Just wanted to clear it up.

1 Like