Skip to content

Instantly share code, notes, and snippets.

@m-esm
Last active January 29, 2020 19:19
Show Gist options
  • Save m-esm/35bc3505478de535e7670599fd213b28 to your computer and use it in GitHub Desktop.
Save m-esm/35bc3505478de535e7670599fd213b28 to your computer and use it in GitHub Desktop.
Check event loop health by checking it's delay
const interval = 1000;
const acceptableDelay = 3;
let lastCheck = 0;
(function checkEventLoop() {
const now = Date.now();
if (lastCheck)
if (now - lastCheck > interval + acceptableDelay) {
console.log('\x1b[31m', 'event loop delay', (now - lastCheck - interval) + 'ms', '\x1b[0m');
} else
console.log('\x1b[32m', 'event loop delay', (now - lastCheck - interval) + 'ms', '\x1b[0m');
lastCheck = Date.now();
setTimeout(() => {
checkEventLoop();
}, interval);
})();
(function initHeavyTask() {
const heavyTask = () => {
console.log('heavyTask start');
for (let i = 0; i < 1e8; i++) {
(Math.sqrt(Math.random() * 1000));
}
console.log('heavyTask end');
setTimeout(() => {
heavyTask();
}, Math.random() * 10000);
}
setTimeout(() => {
heavyTask();
}, 3000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment