Basically, can I just use for i,v in next, instead of for i,v in pairs() in every for loop, since next is faster? Or is there a scenario where I should use pairs/ipairs
next
is faster than pairs
, but only by a tiny amount. Don't use next
instead of pairs
for the purpose of optimizing; use what makes the most sense to you. The reason next
is slightly faster than pairs
is because pairs
is just a call to next
. This is what pairs
really looks like:
function pairs(t) return next, t end
Think of it this way: The atomic weight of an atom doesn't account for the mass of it's electrons; that number is just too small to mean anything. Likewise, programmers should not sacrifice more explicit code when the difference in performance is also too small to mean anything.