Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What's better: a standard "for" loop or a "in pairs" loop?

Just want to know to optimize some of my scripts.

1 answer

✓ Accepted answer

They both are good. Numeric for loops are slightly faster because they don't use function calls. Pairs uses function calls. Pairs is essentially this:

function pairs(x)
	return next,x,nil
end

So when the loop runs, it keeps calling that function thus making it slightly slower. HOWEVER you won't notice much of a difference unless a table has millions of elements inside of it.

This discussion is preserved as it was. New answers, comments, and votes are closed.