stopwatch.js 1.1 KB

12345678910111213141516171819202122232425262728
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { globals } from './platform.js';
  6. const hasPerformanceNow = (globals.performance && typeof globals.performance.now === 'function');
  7. export class StopWatch {
  8. constructor(highResolution) {
  9. this._highResolution = hasPerformanceNow && highResolution;
  10. this._startTime = this._now();
  11. this._stopTime = -1;
  12. }
  13. static create(highResolution = true) {
  14. return new StopWatch(highResolution);
  15. }
  16. stop() {
  17. this._stopTime = this._now();
  18. }
  19. elapsed() {
  20. if (this._stopTime !== -1) {
  21. return this._stopTime - this._startTime;
  22. }
  23. return this._now() - this._startTime;
  24. }
  25. _now() {
  26. return this._highResolution ? globals.performance.now() : Date.now();
  27. }
  28. }