Which for parameter to use?

I’ve seen many people use these two different methods to go through a table. It goes like this:

Option A:

local t = {"a","b","c"}

for i = 1, #t do
    print(t[i])
end

Option B:

local t = {"a","b","c"}

for i,v in pairs(t) do
    print(v)
end

They both kind of do the same thing, I just want to know which one has better performance, or something like that.

I would heavily recommend option B as its more safer
I don’t think there is any difference in performance between the two, but use Option B, as I once had a problem where i was using Option A, and it just would not loop trough the entire thing, only part of it

1 Like

It’s option B because it’s much more clearer than option A.

“for i = 1” method is a traditional method, yet is inefficient because you’re basically looking for every single object at once (yet failing to do so if you have a very big list) + it just looks ugly.

“for i,v in pairs()” method is much better because v is technically t[i], plus it’s very common amongst pro developers (excluding mine tho i might use it in the future).
It’s also safer.

I do use option B all the time, I was just curious. Don’t know if this is allowed but I was just seeing what others use.

1 Like

You should read the rules.
I’m not sure either.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.