UNPKG

actionhero

Version:

The reusable, scalable, and quick node.js API server for stateless and stateful applications

32 lines (31 loc) 977 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.eventLoopDelay = eventLoopDelay; /** * Returns the average delay between a tick of the node.js event loop, as measured for N calls of `process.nextTick` */ async function eventLoopDelay(iterations = 10000) { const jobs = []; async function sleepyFunc() { return new Promise((resolve) => { const start = process.hrtime(); process.nextTick(() => { const delta = process.hrtime(start); const ms = delta[0] * 1000 + delta[1] / 1000000; resolve(ms); }); }); } let i = 0; while (i < iterations) { jobs.push(sleepyFunc); i++; } const results = []; for (const job of jobs) results.push(await job()); let sum = 0; results.forEach((t) => (sum += t)); const avg = Math.round((sum / results.length) * 10000) / 1000; return avg; }