Skip to content

Commit 0f59a39

Browse files
eps1lonSebastian Silbermann
andauthored
fix: Prevent infinite recursion if global.performance is exported ponyfill (oblador#88)
* Add failing test for polyfilled global.performance * fix: Prevent infinite recursion if `global.performance` is exported performance ponyfill * Less access of closure Co-authored-by: Sebastian Silbermann <sebastian.silbermann@klarna.com>
1 parent 03b15a9 commit 0f59a39

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/react-native-performance/src/performance.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
} from './performance-entry';
1212

1313
// @ts-ignore
14-
export const defaultNow = (): number => global.performance.now();
14+
export const defaultNow: () => number = global.performance.now.bind(
15+
// @ts-ignore
16+
global.performance
17+
);
1518

1619
export type MarkOptions = {
1720
startTime?: number;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createPerformance } from '../src/performance';
2+
3+
describe('as a polyfill', () => {
4+
afterEach(() => {
5+
jest.restoreAllMocks();
6+
});
7+
8+
test('performance.now() does not cause infinite recursion', () => {
9+
const { performance } = createPerformance();
10+
// In react-native we can just polyfill the whole global object with `global.performance = performance`
11+
// Doing the same in Node has no effect. The test would pass even without any change to `performance.ts`
12+
jest
13+
// @ts-ignore
14+
.spyOn(global.performance, 'now')
15+
.mockImplementation(performance.now.bind(performance));
16+
17+
// @ts-ignore
18+
expect(() => global.performance.now()).not.toThrow();
19+
});
20+
});

0 commit comments

Comments
 (0)