#More accurate alternative to setInterval()?

1 messages · Page 1 of 1 (latest)

teal slate
#

Hey, I'm writing a server for a multiplayer game with Bun and I'm wondering if there is an alternative to setInterval that does not vary by ~3ms. I've found some stuff online about using setImmediate

Also, I'll be running a WebSocket server at the same time. Will heavy traffic with lots of messages impact the accuracy of the timer?

Thanks!

trail zenith
#

setInterval is up to the system to decide ultimately

#

setImmediate runs on the next microtask tick

#

setTimeout(cb, 0) runs on the next task queue

#

setImmediate runs sooner than setTimeout(cb, 0)

teal slate
trail zenith
#

yes but that is effectively a while(true) loop

teal slate
#

I'm not too experienced with the event loop - will this have a large performance impact

trail zenith
#

with a slight delay

#

your best bet is to schedule something like 2x or 3x more often than necessary