• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

facebook / react-native / 4d9a7017-b8a7-4b9f-88c7-fb35cd561d4b

pending completion
4d9a7017-b8a7-4b9f-88c7-fb35cd561d4b

push

CircleCI

Facebook GitHub Bot
Add PerformanceEventTiming API, according to the standard

3615 of 26397 branches covered (13.69%)

Branch coverage included in aggregate %.

23 of 23 new or added lines in 4 files covered. (100.0%)

7397 of 44564 relevant lines covered (16.6%)

258.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/Libraries/WebPerformance/Performance.js
1
/**
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 *
4
 * This source code is licensed under the MIT license found in the
5
 * LICENSE file in the root directory of this source tree.
6
 *
7
 * @format
8
 * @flow strict
9
 */
10

11
import type {HighResTimeStamp} from './PerformanceEntry';
12

13
import warnOnce from '../Utilities/warnOnce';
×
14
import NativePerformance from './NativePerformance';
×
15
import {PerformanceEntry} from './PerformanceEntry';
×
16

17
type DetailType = mixed;
18

19
export type PerformanceMarkOptions = {
20
  detail?: DetailType,
21
  startTime?: HighResTimeStamp,
22
};
23

24
declare var global: {
25
  // This value is defined directly via JSI, if available.
26
  +nativePerformanceNow?: ?() => number,
27
};
28

29
const getCurrentTimeStamp: () => HighResTimeStamp = global.nativePerformanceNow
×
30
  ? global.nativePerformanceNow
31
  : () => Date.now();
×
32

33
export class PerformanceMark extends PerformanceEntry {
×
34
  detail: DetailType;
35

36
  constructor(markName: string, markOptions?: PerformanceMarkOptions) {
×
37
    super({
×
38
      name: markName,
39
      entryType: 'mark',
40
      startTime: markOptions?.startTime ?? getCurrentTimeStamp(),
×
41
      duration: 0,
42
    });
43

44
    if (markOptions) {
×
45
      this.detail = markOptions.detail;
×
46
    }
×
47
  }
×
48
}
49

50
export type TimeStampOrName = HighResTimeStamp | string;
51

52
export type PerformanceMeasureOptions = {
53
  detail?: DetailType,
54
  start?: TimeStampOrName,
55
  end?: TimeStampOrName,
56
  duration?: HighResTimeStamp,
57
};
58

59
export class PerformanceMeasure extends PerformanceEntry {
×
60
  detail: DetailType;
61

62
  constructor(measureName: string, measureOptions?: PerformanceMeasureOptions) {
×
63
    super({
×
64
      name: measureName,
65
      entryType: 'measure',
66
      startTime: 0,
67
      duration: measureOptions?.duration ?? 0,
×
68
    });
69

70
    if (measureOptions) {
×
71
      this.detail = measureOptions.detail;
×
72
    }
×
73
  }
×
74
}
75

76
function warnNoNativePerformance() {
77
  warnOnce(
×
78
    'missing-native-performance',
79
    'Missing native implementation of Performance',
80
  );
81
}
82

83
/**
84
 * Partial implementation of the Performance interface for RN,
85
 * corresponding to the standard in
86
 *  https://www.w3.org/TR/user-timing/#extensions-performance-interface
87
 */
88
export default class Performance {
×
89
  mark(
90
    markName: string,
91
    markOptions?: PerformanceMarkOptions,
92
  ): PerformanceMark {
93
    const mark = new PerformanceMark(markName, markOptions);
×
94

95
    if (NativePerformance?.mark) {
×
96
      NativePerformance.mark(markName, mark.startTime, mark.duration);
×
97
    } else {
98
      warnNoNativePerformance();
×
99
    }
100

101
    return mark;
×
102
  }
103

104
  clearMarks(markName?: string): void {
105
    if (!NativePerformance?.clearMarks) {
×
106
      warnNoNativePerformance();
×
107
      return;
×
108
    }
109

110
    NativePerformance.clearMarks(markName);
×
111
  }
112

113
  measure(
114
    measureName: string,
115
    startMarkOrOptions?: string | PerformanceMeasureOptions,
116
    endMark?: string,
117
  ): PerformanceMeasure {
118
    let options;
119
    let startMarkName,
120
      endMarkName = endMark,
×
121
      duration,
122
      startTime = 0,
×
123
      endTime = 0;
×
124

125
    if (typeof startMarkOrOptions === 'string') {
×
126
      startMarkName = startMarkOrOptions;
×
127
    } else if (startMarkOrOptions !== undefined) {
×
128
      options = startMarkOrOptions;
×
129
      if (endMark !== undefined) {
×
130
        throw new TypeError(
×
131
          "Performance.measure: Can't have both options and endMark",
132
        );
133
      }
134
      if (options.start === undefined && options.end === undefined) {
×
135
        throw new TypeError(
×
136
          'Performance.measure: Must have at least one of start/end specified in options',
137
        );
138
      }
139
      if (
×
140
        options.start !== undefined &&
×
141
        options.end !== undefined &&
142
        options.duration !== undefined
143
      ) {
144
        throw new TypeError(
×
145
          "Performance.measure: Can't have both start/end and duration explicitly in options",
146
        );
147
      }
148

149
      if (typeof options.start === 'number') {
×
150
        startTime = options.start;
×
151
      } else {
152
        startMarkName = options.start;
×
153
      }
154

155
      if (typeof options.end === 'number') {
×
156
        endTime = options.end;
×
157
      } else {
158
        endMarkName = options.end;
×
159
      }
160

161
      duration = options.duration ?? duration;
×
162
    }
163

164
    const measure = new PerformanceMeasure(measureName, options);
×
165

166
    if (NativePerformance?.measure) {
×
167
      NativePerformance.measure(
×
168
        measureName,
169
        startTime,
170
        endTime,
171
        duration,
172
        startMarkName,
173
        endMarkName,
174
      );
175
    } else {
176
      warnNoNativePerformance();
×
177
    }
178

179
    return measure;
×
180
  }
181

182
  clearMeasures(measureName?: string): void {
183
    if (!NativePerformance?.clearMeasures) {
×
184
      warnNoNativePerformance();
×
185
      return;
×
186
    }
187

188
    NativePerformance.clearMeasures(measureName);
×
189
  }
190

191
  /**
192
   * Returns a double, measured in milliseconds.
193
   * https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
194
   */
195
  now(): HighResTimeStamp {
196
    return getCurrentTimeStamp();
×
197
  }
×
198
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc