Calling many Heartbeats in your game

So, the question is simple, calling lots of “RunService.Heartbeat:Connect()” in your game, would cause performance impacts?

Like 50 scripts calling RunService.Heartbeat, that would be bad?

And It would be the same for RunService.RenderStepped?

4 Likes

I think it doesn’t affect the performance because internally they’re all connected to the same loop and thread. The only thing I can think of is the memory usage being a bit higher due to all the connections.

Also, it might be a bit less efficient since the CPU has to go through more clock cycles calling all the functions (connections) and such opposed to running the code in the same scope. But that’s negligible at this scale.

(I might be wrong though.)

3 Likes

This isn’t a good thing to do as you wanna keep the amount of these as low as possible. I usually use 3 at max, and I will put the stuff I need into the same event.
Example:
A bad way:

-- Script 1
game:GetService('RunService').RenderStepped:Connect(function()
print('This is a print')
end)

-- Script 2
game:GetService('RunService').RenderStepped:Connect(function()
print('This is also a print')
end)

Instead of using 2 separate script, try to put them together!

game:GetService('RunService').RenderStepped:Connect(function()
print('This is a print')
print('This is also a print')
end)

As you can see, they print the same thing, and fewer lines were used. I don’t think calling a lot would lag your game, but combining them saves a lot of time makes your game smoother.

4 Likes

The performance overhead is equivalent to function call overhead * number of functions
All heartbeat is doing is calling the functions you connect to the event. The only difference between sticking it in 50 functions and 1 function is the overhead of calling a function in general. (And I suppose minor memory differences)

There are many more optimizations you could do before you ever need to worry about micro-optimizations like function overhead.

This applies to basically events in general. You’re gonna want to break them up though to make your code more readable and clean. Separating concerns is much harder with just 1 or 2 functions.

4 Likes