I did some research to find out whether or not using variables has any effect on performance when accessing objects that require multiple steps to reach, but multiple sources said different things and weren’t very clear. Here is an example where I wait for a remote event:
To me, I’d expect that by using variables to shorten the instruction, the server would have to perform fewer instructions because the connection is already established, but to know that I’d have to pull studio’s code apart and I don’t program C. Here is how it could be shortened:
local event = game:GetService(“ReplicatedStorage”):WaitForChild(“RemoteEvent”)
event.OnServerEvent:Connect(function()
All I know is one source claimed that shortening it does nothing at all and it just serves as what is effectively a shorter string for the object reference. Honestly, it could be either, but to me this could also mean that when the event is triggered the server has to follow fewer references to reach the target object. Does anyone know whether or not this impacts performance?
If it doesn’t impact performance, you could technically argue that by using the variables you are harming performance by using server/client memory to store the connections, although it will definitely be a very small amount. I’m not too sure, but im not too bothered either way and I actually find the longer instructions easier to read since it shows the exact path you’re coming from instead of having to go and find the variable somewhere else in the script, but I definitely use them for instances, clones and customisable values.
Interesting, clearly this isnt widely agreed upon although waitforchild instead of a direct access I can definitely understand, meaning game:GetService(“ReplicatedStorage”).RemoteEvent
Indeed if you plan to use a variable only once there is no need to make it a variable to be stored by the script but if its being used multiple times it is better.
Because after reading that one post loads of my scripts are littered with extremely long access routes since I refused to use variables unless absolutely necessary, so knowing that for future scripts will help and making sure your code has the highest performance as possible is very important.
Variables store information in memory, so it’s a lot more efficient for you to retrieve information from there then perform multiple function calls to retrieve data. Also, it’s just easier to read and a better practice in general.
game.Players.LocalPlayer:WaitForChild(“PlayerGui”).JetpackMeter.FuelCount.Visible = not game.Players.LocalPlayer:WaitForChild(“PlayerGui”).JetpackMeter.FuelCount.Visible
Well, I still wasn’t sure and decided that I should try using a script to solve it. I am not sure how accurate these numbers are, but these were my findings. Using the following code in two different localscripts:
local text = game:GetService(“Workspace”):WaitForChild(“Part”).box.head.Position
text += Vector3.new(math.random(1,10),math.random(1,10),math.random(1,10))
In two separate localscripts, I repeated this experiment 40 times and compiled the start and end times and found the mean:
Without variables: 0.0006507303598401186 seconds
With variables: 0.0000002187 seconds
As far as I can tell, using variables is considerably faster. I assume that these operations have a similar effect and time on server scripts. When looking at the times I got individually they often varied, sometimes either was faster, I suppose it depends on the CPU. For finding the mean, I put the results into ChatGPT because the results I got were in list format and writing python code to reformat and find the mean would have taken more time than necessary. My point is that I’m not sure if the timings are too different to seem realistic, I would like to know what you guys think.
Yes, please, for the love of god, use variables. There is LITERALLY NO REASON not to use them.
local player = game:GetService("Players").LocalPlayer
local fuelCount = player:WaitForChild("PlayerGui").JetpackMeter.FuelCount
...
fuelCount.Visible = not fuelCount.Visible
Reasons why the “using variables” implementation is faster:
You’re not calling WaitForChild 95 times
You’re not using GetService 95 times.
You’re incrementing a local variable, not a Part’s Position property.