@livy/util
Version:
Common utilities for the Livy logger
60 lines (59 loc) • 1.51 kB
JavaScript
;
/**
* A tiny and simple performance measurement tool for Node.js and the browser, measuring elapsed time in milliseconds
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Timer = exports.NodeTimer = void 0;
/**
* An common abstract timer with some base methods
*/
class BaseTimer {
running() {
return this.started !== null;
}
reset() {
const value = this.get();
this.started = null;
return value;
}
}
/**
* A Node.js timer implementation (hrtime based)
*/
class NodeTimer extends BaseTimer {
constructor() {
super(...arguments);
this.started = null;
}
start() {
this.started = process.hrtime.bigint();
}
get() {
return this.started !== null
? Number(process.hrtime.bigint() - this.started) / 10 ** 6
: null;
}
}
exports.NodeTimer = NodeTimer;
// istanbul ignore next: The Performance API is not properly implemented in JSDOM
/**
* A browser timer implementation (Performance API based)
*/
class BrowserTimer extends BaseTimer {
constructor() {
super(...arguments);
this.started = null;
}
start() {
this.started = performance.now();
}
get() {
return this.started !== null
? Number(performance.now() - this.started) / 10 ** 6
: null;
}
}
/**
* Export the appropriate timer class
*/
exports.Timer = typeof process === 'object' ? NodeTimer : BrowserTimer;