ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
43 lines (42 loc) • 1.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.startDurationMeasurement = void 0;
function startDurationMeasurement() {
// certain environments may not have the performance API:
return globalThis.performance != null
? new PerformanceNowDurationMeasurement()
: new DateDurationMeasurement();
}
exports.startDurationMeasurement = startDurationMeasurement;
class PerformanceNowDurationMeasurement {
constructor() {
Object.defineProperty(this, "startTime", {
enumerable: true,
configurable: true,
writable: true,
value: globalThis.performance.now()
});
}
get startEpochSeconds() {
return Math.floor((globalThis.performance.timeOrigin + this.startTime) / 1000);
}
get durationInMs() {
return Math.ceil(globalThis.performance.now() - this.startTime);
}
}
class DateDurationMeasurement {
constructor() {
Object.defineProperty(this, "startTime", {
enumerable: true,
configurable: true,
writable: true,
value: Date.now()
});
}
get startEpochSeconds() {
return Math.floor(this.startTime / 1000);
}
get durationInMs() {
return Date.now() - this.startTime;
}
}