✓ Accepted answer
In my micro-benchmarks in Lua 5.1 in the command line (which isn't going to be exactly the same as ROBLOX) I get that inside the loop is about 5% faster.
However, these sorts of things shouldn't really make a difference. This is called microoptimizations and worrying about them wastes your time.
The things that make your code fast are algorithmic improvements, not making getting or setting a variable 4% faster. That is not where you are spending your time. You are spending your time traversing lists and computing large objects.
Defining variables in the narrowest scope possible is good practice, so that's what you should do. You shouldn't really expect a difference in performance because they're all stack allocated anyway. I can do some digging as to why one might be faster than the other, but you really shouldn't have to worry about the answer.
2 comments
Benchmark it yourself...
start = tick()before the loop, and thenprint(tick() - start)after.I did once, and the first was faster, but someone in the forums said that it was faster to localize it inside the loop. I just tried it again with SomeFunction being recursive, and outside of the loop is much faster. Thanks for the suggestion.