What is the fastest wait function?

not quite a “wait”, but there’s task.defer
it can go pretty fast for some odd reason that i don’t know about
downside is that you can’t stack defers past 80 defers

for some reason 80 defers is still much less than 1/60th of a second (at least in the server)

if we add in parallel coding, you can use this hacky method with task.desynchronize and task.synchronize to bypass this defer limit
funny

yeah, i literally said before that task.defer exist. But i didn’t know about the defer limit bypass, thanks bro!

Dont you mean RunService? I dont think renderservice is even a thing

:sweat: I think it autocorrected but yes, I mean RunService.Heartbeat

-- [[speed is in how many repeats of the loop we want to pass until yield]]
if i % speed == 0 then task.wait(0) end

Just use multithreading then you don`t have to wait

task.spawn(function()
      while true do
             --no waiting required because the rest is executed in parallel
      end
end)

More examples please? Never tried parallel

@Linex_404’s example isn’t actual parallel, it’s “threading”. Basically, the task scheduler will run threads one after the other, so when one thread yields, another thread starts execution, and this gives the impression that threads are running independent of each other as if they were in “parallel”. However, only 1 thread can run at a time, so different threads still run in serial, but they don’t block each other

With parallel lua, you have multiple cpu threads doing work at the exact same time

The two are often confused (or it is unclear which one is being talked about), sometimes because people don’t understand it, but also because the terminology between the two overlap, which makes it quite confusing

task.defer, puts a function onto the next engine resumption step, (not necessarily heartbeat). it sometimes runs within the same step as well depending on how much work was done

but it can cause stack overflows as its faster than the stack is swept so be careful using it.

cc. @Linex_404 Roblox is single threaded, resumed coroutines run when the active one yields (pauses from a function like task.wait).

noticed that too, but thanks
(char limit)

task.wait() is more precise then the wait() function, so you should use that instead. In regards to your question in what is the fastest “wait” function that yields depends on what you are trying to do. For example, if you are trying to create frame-precise actions you can use game:GetService("RunService").RenderStepped which fires every time before a frame renders.

1 Like

task.wait is equivalent to runservice.heartbeat:wait()

I know about threading though (Which I some times use because idk)

local function FastWait()
	-- the fastest wait function
end


local clock = os.clock()

FastWait()

print("Waited", string.format("%.7f", os.clock() - clock), "seconds")

this should be the fastest wait function
for me this prints Waited 0.0000005 seconds
I don’t think its possible to make it any faster

1 Like