Skip to content

Commit 5a79ece

Browse files
rubennortefacebook-github-bot
authored andcommitted
Use structuredClone to copy detail field in performance.mark and performance.measure (#52483)
Summary: Pull Request resolved: #52483 Changelog: [internal] This is a small refinement for `performance.mark` and `performance.measure` to use `structuredClone` to copy the value of the `detail` field, instead of assigning it by reference. This still doesn't completely fix the semantics of `performance.mark` and `performance.measure`, as the entries returned by those methods aren't referentially equal to the entries reported by `PerformanceObserver` (and the latter don't have the `detail` field yet). Reviewed By: huntie Differential Revision: D77863037 fbshipit-source-id: 54d959612ecd560250e49bb0887bb12112a0142f
1 parent f000116 commit 5a79ece

3 files changed

Lines changed: 21 additions & 12 deletions

File tree

packages/react-native/src/private/webapis/performance/Performance.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
import type {DetailType, PerformanceMarkOptions} from './UserTiming';
1919

2020
import DOMException from '../errors/DOMException';
21+
import structuredClone from '../structuredClone/structuredClone';
2122
import {setPlatformObject} from '../webidl/PlatformObjects';
2223
import {EventCounts} from './EventTiming';
2324
import {
@@ -122,6 +123,11 @@ export default class Performance {
122123
);
123124
}
124125

126+
let resolvedDetail;
127+
if (markOptions?.detail != null) {
128+
resolvedDetail = structuredClone(markOptions.detail);
129+
}
130+
125131
let computedStartTime;
126132
if (NativePerformance?.markWithResult) {
127133
let resolvedStartTime;
@@ -152,7 +158,7 @@ export default class Performance {
152158

153159
return new PerformanceMark(markName, {
154160
startTime: computedStartTime,
155-
detail: markOptions?.detail,
161+
detail: resolvedDetail,
156162
});
157163
}
158164

@@ -249,7 +255,10 @@ export default class Performance {
249255
);
250256
}
251257

252-
resolvedDetail = startMarkOrOptions.detail;
258+
const detail = startMarkOrOptions.detail;
259+
if (detail != null) {
260+
resolvedDetail = structuredClone(detail);
261+
}
253262

254263
break;
255264
}

packages/react-native/src/private/webapis/performance/__tests__/Performance-itest.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ describe('Performance', () => {
7575
});
7676

7777
expect(mark.detail).toEqual(originalDetail);
78-
// TODO structuredClone
79-
// expect(mark.detail).not.toBe(originalDetail);
78+
expect(mark.detail).not.toBe(originalDetail);
8079
});
8180

8281
it('throws if no name is provided', () => {
@@ -423,8 +422,7 @@ describe('Performance', () => {
423422
});
424423

425424
expect(measure.detail).toEqual(originalDetail);
426-
// TODO structuredClone
427-
// expect(measure.detail).not.toBe(originalDetail);
425+
expect(measure.detail).not.toBe(originalDetail);
428426
});
429427
});
430428

packages/react-native/src/private/webapis/performance/__tests__/UserTimingAPI-itest.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ describe('User Timing API', () => {
3232

3333
expect(callback).not.toHaveBeenCalled();
3434

35-
const mark1Detail = Symbol('mark1Detail');
35+
// const mark1Detail = {mark1: 'detail1'};
3636
performance.mark('mark1', {
3737
startTime: 100,
38-
detail: mark1Detail,
38+
// detail: mark1Detail,
3939
});
4040

41-
const mark2Detail = Symbol('mark2Detail');
41+
// const mark2Detail = {mark2: 'detail2'};
4242
performance.mark('mark2', {
4343
startTime: 200,
44-
detail: mark2Detail,
44+
// detail: mark2Detail,
4545
});
4646

4747
expect(callback).not.toHaveBeenCalled();
@@ -61,14 +61,16 @@ describe('User Timing API', () => {
6161
expect(mark1.startTime).toBe(100);
6262
expect(mark1.duration).toBe(0);
6363
// This doesn't work through PerformanceObserver yet
64-
// expect(mark1.detail).toBe(mark1Detail);
64+
// expect(mark1.detail).toEqual(mark1Detail);
65+
// expect(mark1.detail).not.toBe(mark1Detail);
6566

6667
expect(mark2.entryType).toBe('mark');
6768
expect(mark2.name).toBe('mark2');
6869
expect(mark2.startTime).toBe(200);
6970
expect(mark2.duration).toBe(0);
7071
// This doesn't work through PerformanceObserver yet
71-
// expect(mark2.detail).toBe(mark2Detail);
72+
// expect(mark2.detail).toEqual(mark2Detail);
73+
// expect(mark2.detail).not.toBe(mark2Detail);
7274
});
7375
});
7476
});

0 commit comments

Comments
 (0)