Welcome to atomic-sleep ⏱️
Zero CPU overhead, zero dependency, true event-loop blocking sleep
const sleep =console
console.time will report a time of just over 1000ms despite the
being 100ms. This is because the event loop is paused for 1000ms and the setTimeout
fires immediately after the event loop is no longer blocked (as more than 100ms have passed).
Node and Browser versions that support both
Atomics will have (virtually) zero CPU overhead sleep.
For Node, Atomic Sleep can provide zero CPU overhead sleep from Node 8 and up.
For older Node versions and olders browsers we fall back to blocking the event loop in a way that will cause a CPU spike.
👤 David Mark Clements (@davidmarkclem)