skutter
Version:
Skutter: Opinionated BDD Browser based test framework
40 lines (39 loc) • 1.02 kB
JavaScript
;
function delta(startTime) {
if (!startTime) {
throw new Error("[StopWatch].[delta] start not yet called.");
}
let secondsAndNano = process.hrtime(startTime);
let seconds = secondsAndNano[0];
let nano = secondsAndNano[1];
return (nano / 1000000) + (seconds * 1000);
}
class StopWatch {
constructor() {
this.startTime = null;
this.startTime = null;
this.elapsed = 0;
}
start() {
this.startTime = process.hrtime();
}
stop() {
if (!this.startTime) {
throw new Error("[StopWatch].[stop] start not yet called.");
}
this.elapsed = delta(this.startTime);
}
get elapsedMilliseconds() {
if (!this.startTime) {
return 0;
}
if (this.elapsed !== 0) {
return this.elapsed;
}
return delta(this.startTime);
}
get elapsedSeconds() {
return this.elapsedMilliseconds / 1000;
}
}
exports.StopWatch = StopWatch;