✓ Accepted answer
I’m not amazingly good with the lower levels of languages, but I am 90% sure that variable assignment only takes time at compile time. which is to say, when it is converted to bytecode, the memory for variables is already preallocated. not only that, but reused variables can be detected and optimized automatically, making no difference! in fact, the second one is even better because even after optimization the variable is found in an enclosed scope and it does not affect future processes with extra variables to look through. more importantly it is just easier to read.
note that this kind of thing is so miniscule that just having the variable outside and gobbling memory is slower than assigning it
you should never worry about variable assignments unless you are directly assigning memory yourself. which you do not do in lua.
edit: I would also like to mention which I find pretty neat, that the same goes for mathematical operations that only use constants. for example, you will never need to store Vector3.new(0, 1, 0) or math.sqrt, you could actually use than IN your equation, because they will be automatically converted to constants only once!
1 comment
In my opinion, the First(A) is better when it comes to low-spec devices. Instead of storing a new data in the local RAM every time this event is fired, I prefer to store the local value only once, thus saving the RAM cashed memory drastically. This both won't make any difference when it comes to high-end devices. So, I highly recommend using the First(A) because to ensure the stability of the RAM.