Table differences

So, most of us know you can’t get a table and directly do something to it unless you loop through everything in the table.
My question is, which method should I use and what are the differences?

local yes = game.Workspace.Noob:GetChildren()
for i = 1, #yes do
      yes[i]:Destroy()
end

I’m not sure if the following method is the correct way to use it, as I currently use for i = 1, #thing do

local yes = game.Workspace.Noob:GetChildren()
for  _,v in pairs(yes) do
      v:Destroy()
end

Yeah that’s definitely wrong somehow…

You have to write it as pairs(table) instead of just pairs.
For your main question, you can use both of them if table only has index-value pairs but you have to use pairs() if you have key-value pairs in your table and you want to get those key-value pairs with a loop.

2 Likes

I personally use the

for i = 1, #yes do

end

I’ve had someone tell me this was a better option.

You can check this by running through the table a bunch of times for each type then printing the time it took to do it.

local startTime = os.time()
for i = 1, 10000 do
    for i = 1, #yes do

    end
end
print(os.time() - startTime)

My guess to the reason that this is: is because in pairs() gives you more to work with

local Table = {
["One"] = 15,
["Two"] = 12,
["Three"] = 10
}

Using in pairs will give you the key and the value for each iteration. (“One”, 15)

Using for i = 1, #yes will just give you the current iteration number, so if that’s all you need it’s more efficient. (1)

Please correct me if anything I said is incorrect.

2 Likes

I realized I went off on a tangent and branched off of the subject of the post above mine. I apologize :zipper_mouth_face:

Well my common use for tables is :GetChildren().

Hello!

Just a bit of technicalities! The two types of for loops mentioned in this topic of discussion are the numeric for loops and the generic for loops.

The numeric for loop takes on the following syntax:

for var=exp1,exp2,exp3 do
  something
end

While the generic for loop takes on the following syntax:

for i,v in ipairs(a) do
  print(v)
end

When iterating over tables in roblox, there is no functional difference to the eventual output. However, when it comes to performance, using the numeric for loop consistently triumphs over the generic for loop. (i.e. The numeric for loop is faster.) That being said, you would only really see those effects when you are iterating over extremely large tables or performing rather complex operations over every iteration. Furthermore, there are those that “claim” that the generic for loop looks “cleaner” than the numeric for loop.

As @polill00 has mentioned, you can certainly test this out yourself in studio!

In conclusion, if you are after performance, I would recommend the numeric for loop. If you are not a stickler for that, then either form would suffice! Hope this helps!

EDIT: What I mentioned still holds true to a certain extent. HOWEVER, since last year, roblox released a new lua interpreter. Both methods of for looping are now comparable even with regards to performance, BUT the generic for loop is now slightly faster. I would relinquish the conclusion drawn previously, but just keep in mind that using either for loop would really not make that much of a difference in terms of performance!

EDIT 2: Here is a link to a devforum post in which the author performed an empirical benchmark study on the speeds of each for loop. The results are consistent with what was said above! Speed comparison : ipairs vs pairs vs numerical loop [benchmark]

2 Likes