29 lines
1.1 KiB
JavaScript
29 lines
1.1 KiB
JavaScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
import { globals } from './platform.js';
|
|
const hasPerformanceNow = (globals.performance && typeof globals.performance.now === 'function');
|
|
export class StopWatch {
|
|
constructor(highResolution) {
|
|
this._highResolution = hasPerformanceNow && highResolution;
|
|
this._startTime = this._now();
|
|
this._stopTime = -1;
|
|
}
|
|
static create(highResolution = true) {
|
|
return new StopWatch(highResolution);
|
|
}
|
|
stop() {
|
|
this._stopTime = this._now();
|
|
}
|
|
elapsed() {
|
|
if (this._stopTime !== -1) {
|
|
return this._stopTime - this._startTime;
|
|
}
|
|
return this._now() - this._startTime;
|
|
}
|
|
_now() {
|
|
return this._highResolution ? globals.performance.now() : Date.now();
|
|
}
|
|
}
|