precise
Version:
Precision timer for nanosecond differences
80 lines (60 loc) • 1.46 kB
JavaScript
/**
* precise
*
* @copyright 2023 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 4.0.3
*/
'use strict';
var node_process = require('node:process');
const STARTED = "Timer has been started";
const STOPPED = "Timer has been stopped";
const NOT_STARTED = "Timer has not been started";
const NOT_STOPPED = "Timer has not been stopped";
const BIG_INT_NEG_1 = BigInt(-1);
class Precise {
constructor () {
this.started = BIG_INT_NEG_1;
this.stopped = BIG_INT_NEG_1;
}
diff (ms = false) {
if (this.started === BIG_INT_NEG_1) {
throw new Error(NOT_STARTED);
}
if (this.stopped === BIG_INT_NEG_1) {
throw new Error(NOT_STOPPED);
}
let result = Number(this.stopped - this.started);
if (ms) {
result = parseInt(result / 1e6, 10);
}
return result;
}
reset () {
this.started = BIG_INT_NEG_1;
this.stopped = BIG_INT_NEG_1;
return this;
}
start () {
if (this.started > BIG_INT_NEG_1) {
throw new Error(STARTED);
}
this.started = node_process.hrtime.bigint();
return this;
}
stop () {
if (this.started === BIG_INT_NEG_1) {
throw new Error(NOT_STARTED);
}
if (this.stopped > BIG_INT_NEG_1) {
throw new Error(STOPPED);
}
this.stopped = node_process.hrtime.bigint();
return this;
}
}
function precise () {
return new Precise();
}
exports.Precise = Precise;
exports.precise = precise;