Performance question about :Wait() and connections

So my friend is crazy for micro-optimization and this is usually what he does:

spawn(function()

while true do

game:GetService("RunService").Heartbeat:Wait()

end

end)

Basically he’s saying that:

game:GetService("RunService").Heartbeat:Connect(function()

end)

is worse then what he provided above. His argument is that the second piece of code creates a new thread each time where the first one doesn’t. So is this true and should the second one be avoided?

I believe if you print the thread for the RunService one it’ll always be the same thread, since Roblox reuses it. There’s really no performance to be gained out of this.

8 Likes

I always find that without a wait(), you significantly lag.

Why not use something like
wait(0.0003)

in the while loop, or else it creates quite a bit of slowing, especially for those with a potato PC.

If anything, that first one is worse. It’s using a function which has legacy holdovers and it’s is pretty bad in a majority of circumstances. Both of them still thread in relatively the same way and the event connection is, as earlier mentioned, reused. A new one is not created every time its fired.

There’s only a use difference between Wait and Connect, not in how they create threads. Wait will wait until the event fires and then return all the arguments that were passed to the event. Connect allows you to make a function, the same one, get called whenever the event is fired.

Not to mention, the first one looks terrible for readability. While there’s no significant difference at all to get here, sacrificing readability for silly microoptimisation, even if you’re a fanatic for it, is no good.

1 Like

The first one is far worse, connecting a function to an event will always hold supreme over a while loop. The second one is far more practical, logical, and usable in the case you wanted to stop it from recurring.

It’s additionally worth noting that those two code examples are not functionally equivalent. The first example will miss the next signal fire if it’s still dealing with the prior. This isn’t always a con, and depending on what you’re doing, can either be beneficial, or detrimental.

If you’re going to link my thread, I ask that you please make sure it is relevant, considering the rest of the thread is about using the :Wait() method the OP is using.

1 Like