Skip to content

Commit

Permalink
Hardened performance.now() check a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed Sep 7, 2018
1 parent ba86c9f commit 0b9fc7f
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions plugins/Profiler/ProfileCollector.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ type Agent = any;

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

let performanceNow;
if (
typeof performance !== 'undefined' &&
typeof performance.now === 'function'
) {
performanceNow = () => performance.now();
} else {
performanceNow = () => Date.now();
}
const hasNativePerformanceNow =
typeof performance === 'object' &&
typeof performance.now === 'function';

const now = hasNativePerformanceNow
? () => performance.now()
: () => Date.now();

/**
* The Profiler UI displays the entire React tree, with timing info, for each commit.
Expand Down Expand Up @@ -65,7 +63,7 @@ class ProfileCollector {
const storeSnapshot: StoreSnapshot = {
memoizedInteractions,
committedNodes: Array.from(this._committedNodes),
commitTime: performanceNow() - this._recordingStartTime,
commitTime: now() - this._recordingStartTime,
duration: this._maxActualDuration,
root: id,
};
Expand All @@ -76,7 +74,7 @@ class ProfileCollector {
_onIsRecording = isRecording => {
this._committedNodes = new Set();
this._isRecording = isRecording;
this._recordingStartTime = isRecording ? performanceNow() : 0;
this._recordingStartTime = isRecording ? now() : 0;

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

0 comments on commit 0b9fc7f

Please sign in to comment.