JSPM

event-loop-sleep

1.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q34652F
  • License MIT

Zero CPU overhead, zero dependency, true event-loop blocking sleep. Based on the abandoned atomic-sleep project.

Package Exports

  • event-loop-sleep
  • event-loop-sleep/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (event-loop-sleep) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Welcome to event-loop-sleep ⏱️

Version License: MIT

Zero CPU overhead, zero dependency, true event-loop blocking sleep

Usage

const sleep = require("event-loop-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

npm install

Run tests

npm test

Support

Node and Browser versions that support both SharedArrayBuffer and Atomics will have (virtually) zero CPU overhead sleep.

For Node, Event Loop 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.


Based on the original work of David Mark Clements.