Skip to content

Commit

Permalink
fix(apm): Remove Performance references (#2495)
Browse files Browse the repository at this point in the history
* fix: Calling Performance to not require dom

* meta: Changelog

* ref: Type
  • Loading branch information
HazAT authored Mar 13, 2020
1 parent ef879bf commit 13ab8e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott

- [apm] fix: Use Performance API for timings when available, including Web Workers (#2492)
- [apm] fix: Remove Performance references (#2495)

## 5.14.1

Expand Down
17 changes: 14 additions & 3 deletions packages/utils/src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,18 @@ function _htmlElementAsString(el: unknown): string {
const INITIAL_TIME = Date.now();
let prevNow = 0;

const performanceFallback: Pick<Performance, 'now' | 'timeOrigin'> = {
/**
* Cross platform compatible partial performance implementation
*/
interface CrossPlatformPerformance {
/**
* Returns the current timestamp in ms
*/
now(): number;
timeOrigin: number;
}

const performanceFallback: CrossPlatformPerformance = {
now(): number {
let now = Date.now() - INITIAL_TIME;
if (now < prevNow) {
Expand All @@ -361,10 +372,10 @@ const performanceFallback: Pick<Performance, 'now' | 'timeOrigin'> = {
timeOrigin: INITIAL_TIME,
};

export const crossPlatformPerformance: Pick<Performance, 'now' | 'timeOrigin'> = (() => {
export const crossPlatformPerformance: CrossPlatformPerformance = (() => {
if (isNodeEnv()) {
try {
const perfHooks = dynamicRequire(module, 'perf_hooks') as { performance: Performance };
const perfHooks = dynamicRequire(module, 'perf_hooks') as { performance: CrossPlatformPerformance };
return perfHooks.performance;
} catch (_) {
return performanceFallback;
Expand Down

0 comments on commit 13ab8e5

Please sign in to comment.