mono/packages/core/dist/stopwatch.js
2025-01-28 13:42:22 +01:00

32 lines
1.2 KiB
JavaScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
const hasPerformanceNow = (globalThis.performance && typeof globalThis.performance.now === 'function');
export class StopWatch {
_startTime;
_stopTime;
_now;
static create(highResolution) {
return new StopWatch(highResolution);
}
constructor(highResolution) {
this._now = hasPerformanceNow && highResolution === false ? Date.now : globalThis.performance.now.bind(globalThis.performance);
this._startTime = this._now();
this._stopTime = -1;
}
stop() {
this._stopTime = this._now();
}
reset() {
this._startTime = this._now();
this._stopTime = -1;
}
elapsed() {
if (this._stopTime !== -1) {
return this._stopTime - this._startTime;
}
return this._now() - this._startTime;
}
}
//# sourceMappingURL=stopwatch.js.map