Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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