superfly-timeline
Version:
Resolver for defining objects with temporal boolean logic relationships on a timeline
77 lines • 2.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ticTocPrint = exports.tic = exports.setPerformanceTimeFunction = exports.performance = exports.activatePerformanceDebugging = void 0;
let durations = {};
let callCounts = {};
let firstStartTime = 0;
let active = false;
function activatePerformanceDebugging(activate) {
active = activate;
}
exports.activatePerformanceDebugging = activatePerformanceDebugging;
exports.performance = {
now: Date.now,
};
/**
* This is a little wierd, but we don't want to import performance from 'perf_hooks' directly,
* because that will cause issues when this library is used in a browser.
* Intended usage:
* import { performance } from 'perf_hooks'
* setPerformanceTimeFunction(performance.now)
* @param now
*/
function setPerformanceTimeFunction(now) {
exports.performance.now = now;
}
exports.setPerformanceTimeFunction = setPerformanceTimeFunction;
function noop() {
// nothing
}
/**
* Used to measure performance.
* Starts a measurement, returns a function that should be called when the measurement is done.
*/
function tic(id) {
if (!active)
return noop;
if (!firstStartTime)
firstStartTime = exports.performance.now();
if (!durations[id])
durations[id] = 0;
if (!callCounts[id])
callCounts[id] = 0;
const startTime = exports.performance.now();
return () => {
const duration = exports.performance.now() - startTime;
durations[id] = durations[id] + duration;
callCounts[id]++;
};
}
exports.tic = tic;
function ticTocPrint() {
if (!active)
return;
const totalDuration = exports.performance.now() - firstStartTime;
const maxKeyLength = Math.max(...Object.keys(durations).map((k) => k.length));
console.log('ticTocPrint\n' +
padStr(`Total duration `, maxKeyLength + 2) +
`${Math.floor(totalDuration)}\n` +
Object.entries(durations)
.map((d) => {
let str = padStr(`${d[0]} `, maxKeyLength + 2);
str += padStr(`${Math.floor(d[1] * 10) / 10}`, 8);
str += padStr(`${Math.floor((d[1] / totalDuration) * 1000) / 10}%`, 7);
str += `${callCounts[d[0]]}`;
return str;
})
.join('\n'));
durations = {};
callCounts = {};
}
exports.ticTocPrint = ticTocPrint;
function padStr(str, length) {
while (str.length < length)
str += ' ';
return str;
}
//# sourceMappingURL=performance.js.map