#Difference in performance?

1 messages · Page 1 of 1 (latest)

clever obsidian
#

i was told that using a for loop is better than using ipairs for looping through a table, it it actually true? or its basically the same or worse.

local list = {1,2,3,4,5}
--method one
for i, v in ipairs(list) do
    print(i, v)
end
--method two
local length = #list
for i = 1, length do
    local v = list[i]
    print(i, v)
end
chrome cargo
#

in terms of performance, i would not worry about it, to be honest. there are mainly other practical reasons why you'd choose pairs/ipairs over the other. see:
https://devforum.roblox.com/t/whats-the-difference-between-ipairs-and-pairs/2576821/2

#

you can also use neither, as generalized iteration has been introduced, but i hear that is worse for performance than the other 2 options (though, unless you have a specific use case, probably negligible)

#

from what i'm seeing, ipairs is faster iterating through arrays, though