Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/davidmarkclements/atomic-sleep
⏱️Zero CPU overhead, zero dependency, true event-loop blocking sleep ⏱️
https://github.com/davidmarkclements/atomic-sleep
Last synced: 7 days ago
JSON representation
⏱️Zero CPU overhead, zero dependency, true event-loop blocking sleep ⏱️
- Host: GitHub
- URL: https://github.com/davidmarkclements/atomic-sleep
- Owner: davidmarkclements
- License: mit
- Created: 2020-03-09T18:48:55.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-03-11T06:58:39.000Z (almost 5 years ago)
- Last Synced: 2025-01-05T14:17:28.249Z (14 days ago)
- Language: JavaScript
- Homepage:
- Size: 3.91 KB
- Stars: 79
- Watchers: 6
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
Welcome to atomic-sleep ⏱️
> Zero CPU overhead, zero dependency, true event-loop blocking sleep
## Usage
```js
const sleep = require('atomic-sleep')console.time('sleep')
setTimeout(() => { console.timeEnd('sleep') }, 100)
sleep(1000)
```The `console.time` will report a time of just over 1000ms despite the `setTimeout`
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).## Install
```sh
npm install
```## Run tests
```sh
npm test
```## Support
Node and Browser versions that support both `SharedArrayBuffer` and `Atomics` will have (virtually) zero CPU overhead sleep.
For Node, Atomic Sleep can provide zero CPU overhead sleep from Node 8 and up.
For browser support see https://caniuse.com/#feat=sharedarraybuffer and https://caniuse.com/#feat=mdn-javascript_builtins_atomics.
For older Node versions and olders browsers we fall back to blocking the event loop in a way that will cause a CPU spike.
## Author
👤 **David Mark Clements (@davidmarkclem)**
* Twitter: [@davidmarkclem](https://twitter.com/davidmarkclem)
* Github: [@davidmarkclements](https://github.com/davidmarkclements)