I don’t see any posts on this. A lot of times I see scripts written in this in table way and I’ve been wanting to know what’s the difference.
local t = {}
for i, v in t do
end
I don’t see any posts on this. A lot of times I see scripts written in this in table way and I’ve been wanting to know what’s the difference.
local t = {}
for i, v in t do
end
pairs/ipairs is almost useless, not using it at all is technically more efficient because less time for the server to parse
Oh, okay. But isn’t ipairs useful for going in order though?
yes if for whatever reason your indices are out of order, otherwise nothing is the same as using pairs()
It’s just shorthand for iterating through key/value pairs in a table. If it’s a dictionary, it’s the same as using pairs(t)
, and if it’s an array (table with number indices, also called a “list-like” table) it’s the same as using ipairs(t)
.
The above comment about being “less time to parse” is incorrect, using the shorthand is technically one more step because it has to determine if pairs or ipairs would be used based on the type of table. – though I could be wrong too, honestly it doesn’t really matter unless you’re dealing with an optimization issue
One last thing: the official Roblox Lua Style guide recommends explicitly using pairs
for dictionary-like tables and ipairs
for list-like tables since it helps the reader know what kind of table is being operated on. It’s best practice to make your code as readable as possible.
Normally you use pairs on a table of instances, like when calling :GetChildren/Descendants (or atleast it’s the most common use case the function returns a table and i guess it’s faster to write
for i,v in pairs(x:GetChildren())
than local t=x:GetChildren() for i,v in t
, but i don’t use tables too much, feels weird working with just a table and no instances
for i, v in t:GetChildren()
do is a lot faster though.
You can call GetChildren() on tables?
Benchmarks are your number one friend
local t = table.create(1e3, 123)
return {
ParameterGenerator = function()
return
end,
Functions = {
["GI"] = function()
for i = 1, 1e2 do
for k, v in t do end
end
end,
["pairs"] = function(Profiler)
for i = 1, 1e2 do
for k, v in pairs(t) do end
end
end,
["ipairs"] = function(Profiler)
for i = 1, 1e2 do
for k, v in ipairs(t) do end
end
end,
["next"] = function(Profiler)
for i = 1, 1e2 do
for k, v in next, t do end
end
end,
},
}
Enabling native Luau (where the code gets compiled into assembly instead of bytecode) yields a similar result but within the margin of error; everything is literally just twice as fast lol
Please read before posting as there’s many duplicate posts for this. TLDR for in do
is the same as using pairs and is (negligably) faster. It’s a Luau exclusive feature. Only use ipairs if you need some of its very specific functionality or use pairs if you need lua backwards compatability.
P.S the style guide was wrote before it was introduced as a language feature @Riesegarder.
Woah really? I’m a little outdated then. Thanks for letting me know, learned something new
You’re intentionally making it longer than it should. This is all you need:
for i, v in x:GetChildren() do
I thought you needed pairs for that to work, the more you know i guess, that does seem to make pairs redundant
No, I meant for an instance. You can’t call :GetChildren()
on tables.
that is not even clsoe to accurate…
this is worong aswell. if your not good at scripting then don’t answer questions, just listen for the professionals responses.
Please don’t let your ego get to you
GI is identical to pairs(), except that it is guaranteed to iterate the array portion of a table in chronological order. After doing that, it will iterate the rest of the table (dictionary) in an arbitrary order. In other words, you can think of it as a combination of pairs
and ipairs
.
There is no practical reason why someone would find GI’s new behavior problematic, so it is safe to assume that pairs
is indeed useless.
However, ipairs
(not pairs
) is still useful for when you only want to iterate the array portion of a table and not the dictionary. It is not completely useless.
if there’s one thing that bugs me with GI
it’s the fact that strict typechecking always slaps me with a warning saying i can’t call non-function (insert table here) or that i need something like pairs
whenever i use it
but otherwise it’s all good
and no, the warning shouldn’t really interfere with anything, but it still bugs me seeing a yellow underline