-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve
measureRenders
precision (#534)
* feat: improve `measureRender` precision * chore: changeset * refactor: self code review
- Loading branch information
1 parent
fafc067
commit 63f1f35
Showing
3 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@callstack/reassure-measure': minor | ||
--- | ||
|
||
improve `measureRenders` precision on React Native |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { performance as perf } from 'perf_hooks'; | ||
import { getTestingLibrary } from './testing-library'; | ||
|
||
export function applyRenderPolyfills() { | ||
const testingLibrary = getTestingLibrary(); | ||
if (testingLibrary === 'react-native') { | ||
polyfillPerformanceNow(); | ||
} | ||
} | ||
|
||
export function revertRenderPolyfills() { | ||
const testingLibrary = getTestingLibrary(); | ||
if (testingLibrary === 'react-native') { | ||
restorePerformanceNow(); | ||
} | ||
} | ||
|
||
/** | ||
* React Native Jest preset mocks the global.performance object, with `now()` method being `Date.now()`. | ||
* Ref: https://github.com/facebook/react-native/blob/3dfe22bd27429a43b4648c597b71f7965f31ca65/packages/react-native/jest/setup.js#L41 | ||
* | ||
* Then React uses `performance.now()` in `Scheduler` to measure component render time. | ||
* https://github.com/facebook/react/blob/45804af18d589fd2c181f3b020f07661c46b73ea/packages/scheduler/src/forks/Scheduler.js#L59 | ||
*/ | ||
let originalPerformanceNow: () => number; | ||
|
||
function polyfillPerformanceNow() { | ||
originalPerformanceNow = global.performance?.now; | ||
global.performance.now = () => perf.now(); | ||
} | ||
|
||
function restorePerformanceNow() { | ||
globalThis.performance.now = originalPerformanceNow; | ||
} |