I’ve heard that for i,v in next, is faster than for i,v in pairs() and for i,v in ipairs() can someone clear up which one is faster and what the differences are
pairs
really just is the next iterator function being used:
function pairs(t)
return next, t
end
In term of performance, they’re about the same - difference would be deemed negligible. ipairs
is more optimised for arrays than pairs is however, ipairs only works on arrays (assuming they are no holes for it to iterate over the entire array). Performance is also deemed negligible with pairs and ipairs but generally ipairs is preferred.
From what I’ve heard, pairs
and ipairs
are actually more optimized over next
due to internal Luau optimizations.
The pairs
iterator can iterate over all the indices of an array along with its keys, while ipairs
(i can stand for index) only work for numerical keys/indices, and won’t iterate over any keys in the table, and also stops iterating at the first nil index.