Runservice Heartbeat or while loops

Which is better?

RunService.Heartbeat:Connect(function()
	local TimeInUnix = os.time() + 10800
	local stringToForm = "%I:%M"
	local result1 = os.date(stringToForm, TimeInUnix)

	lockTime.Text = result1
end)

or

while true do
	local TimeInUnix = os.time() + 10800
	local stringToForm = "%I:%M"

	local result1 = os.date(stringToForm, TimeInUnix)
	lockTime.Text = result1
	task.wait(1)
end

It really depends on how fast you want the updates. A while loop is generally better if you don’t need really fast updates, but if you want speed, heartbeat is the way to go. In the end they are kind of doing the same thing anyways.

2 Likes

And for performance? Which is better?

We are talking incredibly small performance gains if you are trying to fire something quickly. You won’t really be able to tell the difference. If you can add a larger delay, a while loop is better because you aren’t doing the calculations at as high a frequency.

1 Like

Alrighty, thank you so much for your answer. So if only fire quickly = almost the same but if you want to use large delay, it’s better using while loop.

1 Like