I noticed that there’s a difference between running stuff in separate functions and running stuff in one function. To see if this was actually true I made a little script to see if there’s a difference and if so to see what the difference is.
I ran math.sqrt(1e6)
in three different loops under heartbeat. One without any functions one in a function and one in a function in a module script.
This is that script:
local Module = require(script.ModuleScript)
local function Function()
math.sqrt(1e6)
end
game:GetService("RunService").Heartbeat:Connect(function()
debug.profilebegin("Out Function") -- Math in the same function
for i = 1e5, 0, -1 do
math.sqrt(1e6)
end
debug.profileend()
debug.profilebegin("In Function") -- Math in another function
for i = 1e5, 0, -1 do
Function()
end
debug.profileend()
debug.profilebegin("In Module Function") -- Math in a function in a module script
for i = 1e5, 0, -1 do
Module.Function()
end
debug.profileend()
end)
The result was interesting though:
There was actually a huge difference between them, with the one that’s out function being almost 2x as fast as the one that didn’t call the function every time. Although it fires the function 100.000 times, a 2 to 1 ratio is unbelievable.
And to confirm it isn’t just isn’t just one frame I used tick()
to see what the average is over 5 seconds:
Using functions is clearly slower than not using them, but functions are like the main way to structure your scripts and they also prevent writing the same code multiple times.
So how do you write performant scripts without them turning into a hot mess?