Question about variables

It kinda depends. While the Roblox VM is pretty good at keeping memory safe, memory leaks still happen. I am talking about the garbage collector, the thing that is supposed to remove unused variables from memory. When programming in low level languages like C, the compiler will try to optimize your code. For example, it will try to merge variables together. Let’s say we have 3 variables: a = 5, b = 5 and c = a + b. We end up only printing c: print(c). Instead of using all 3 variables for this calculation, it should end up discarding a and b, defining c as 5 + 5. It might even just remove all of these variables because they are not used anywhere else. It will just output print(5+5) without the use of variables.
btw this is on machine code level, I am just giving an example.

It really depends, Roblox also had an update that introduced native machine code generation which is supposed to optimize these stuff even better. Having a lot of variables is completely normal. That is why they exist. For example you have a HTTP request handler. You can fetch the status code with some function. You need like 10 if statements, you gonna put a new fetch for each statement or you gonna save the code in a single variable then check it? While having too many variables can make your code look cluttered with garbage, it doesn’t matter because it ends up going through a lot of rules for optimization.

This is actually an interesting topic, because Roblox’s variables act dumb. If you wanted to write the number 255, it will save it with 3 whole bytes of data. At least people claim that this is the case. I haven’t ever checked or confirmed it myself, but since a lot of community made libraries exist, it is probably true. They are supposed to convert that integer into an actual single byte. This is called bit-packing. You can read more about it on the internet. It might be very useful when data compression and speed is critical.

I am currently doing research and a lot of testing for a project I started some time ago. Basically a “video player”. Just like in that post for bit packing I linked, it’s shown how small mistakes can lead to disasters in performance. I hope you find this topic interesting and useful. Again, it’s completely normal to have lots of variables. It’s not the amount of variables that is critical, but how you use them in your code. This is a complex topic, so no your question is not stupid. It is very important to understand it. Also remember that google is your friend.

2 Likes