Why do we need ipairs?

This is a simple question. Why does ipairs exist? As far as I know, everything you could do with ipairs you could just do with pairs. Does ipairs have some feature or advantage that I’m not aware of?

2 Likes

ipairs is for iterating for arrays in numerical order. This is especially useful when making leaderboards. Normal pairs doesn’t iterate in any particular order which is why it could be unreliable in some situations. ipairs doesn’t make much a difference when it comes to dictionaries though, as their keys consist of strings rather than integers.

8 Likes

To add on to @dukzae, you should use pairs for dictionaries, and ipairs for arrays.

Also, if ipairs encounters nil, it will stop in it’s tracks.

3 Likes

pairs is faster then ipairs

it has its uses but only use ipairs if you actually need it
if you don’t need it just use pairs

This mate provides a really great example as to why ipairs is important, but it’s basically used for ordering an Array mostly:

can ipairs iterate over a dictionary? Also, if it can, will it go in order? What i mean is something like this

local table1 = {
    first = "1",
    second = "2",
    third = "3"
}

for i,v in ipairs(table1)  do
    print(i,v)
end
--would this work, and if it worked,
--would it output this? -> first 1 second 2 third 3

no dictionaries can’t be ordered

Did you mean to say ipairs is faster than pairs?

As for OP’s question, ipairs is equivalent to a numerical loop, making it faster when iterating over large ordered arrays. pairs is used to iterate over tables that aren’t arrays or are unordered.

no I didn’t and I have seen tests proving what I said

and by “faster” I mean performance wise, not how easy they are to use

Take a look at this benchmark I ran just now and tell me what you think. The difference might seem small, but it adds up.

I don’t care about your data
you can’t convince me facts I already know

ipairs is faster in luau but in vanilla lua it is slower then pairs

Well, we’re using Luau, aren’t we? Regardless of whether it does for “vanilla Lua”, we aren’t using vanilla Lua, we’re using an optimized version of it, and it just so happens in this optimized version ipairs is faster. I’d tend to think it’d be a good idea to take advantage of that, but if you refuse to use it for some reason, I guess that’s on you.

I never said in my first comment which version of lua I was talking about
don’t assume things

Were using Luau here, all references about Lua don’t matter.

Besides that’s not even true, ipairs is about 20% faster then pairs in ordinary Lua(Source).

3 Likes

This is going to be the last time I reply because I don’t want to continue bumping the topic.

This is the ROBLOX developer forum. Not the Lua developer forum, not Stack Overflow’s Lua section, not even Scripting Helpers, this is the Scripting Support section of the ROBLOX Developer forum. Of course I assumed you were talking about ROBLOX’s own Luau, because why on earth WOULD you be talking about vanilla Lua here? You’re talking about something Lua-related on a forum that uses a derivative of Lua. Either clarify you’re talking about vanilla Lua (which you’re wrong about anyway, if the reply above me is to be believed) or stop spreading misinformation.

4 Likes

he started it
don’t SMH me and let me relax, I don’t see a reason to respond to me anymore

Can’t be ordered but can be sorted :).

Example of what I mean is:

local ToSort = {["A"] = 15,["B"] = 4,["C"] = 3}
table.sort(ToSort,function(a,b)
    if a < b then
       return a
     end
end)

print(table.unpack(ToSort)) --> should be now sorted from lowest to greatest number

Just gonna say this:

Don’t add more controversy to this issue, it won’t help anyone & would result in more unnecessary arguments from what the OP is asking for

Unless you have something to contribute with valid points & evidence, don’t send a post or either take it in DM’s

Dictionaries can’t be ordered for the reason of having custom “indexes”, which would result in them going through in a randomized order instead of in a chronological order kinda like what pairs does