Skip to content

Commit 0b9fc7f

Browse files
author
Brian Vaughn
committed
Hardened performance.now() check a bit
1 parent ba86c9f commit 0b9fc7f

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

plugins/Profiler/ProfileCollector.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ type Agent = any;
1515

1616
import type {Interaction, StoreSnapshot} from './ProfilerTypes';
1717

18-
let performanceNow;
19-
if (
20-
typeof performance !== 'undefined' &&
21-
typeof performance.now === 'function'
22-
) {
23-
performanceNow = () => performance.now();
24-
} else {
25-
performanceNow = () => Date.now();
26-
}
18+
const hasNativePerformanceNow =
19+
typeof performance === 'object' &&
20+
typeof performance.now === 'function';
21+
22+
const now = hasNativePerformanceNow
23+
? () => performance.now()
24+
: () => Date.now();
2725

2826
/**
2927
* The Profiler UI displays the entire React tree, with timing info, for each commit.
@@ -65,7 +63,7 @@ class ProfileCollector {
6563
const storeSnapshot: StoreSnapshot = {
6664
memoizedInteractions,
6765
committedNodes: Array.from(this._committedNodes),
68-
commitTime: performanceNow() - this._recordingStartTime,
66+
commitTime: now() - this._recordingStartTime,
6967
duration: this._maxActualDuration,
7068
root: id,
7169
};
@@ -76,7 +74,7 @@ class ProfileCollector {
7674
_onIsRecording = isRecording => {
7775
this._committedNodes = new Set();
7876
this._isRecording = isRecording;
79-
this._recordingStartTime = isRecording ? performanceNow() : 0;
77+
this._recordingStartTime = isRecording ? now() : 0;
8078

8179
if (isRecording) {
8280
// Maybe in the future, we'll allow collecting multiple profiles and stepping through them.

0 commit comments

Comments
 (0)