@@ -192,6 +192,63 @@ function resumeCurrentLifeCycleTimer() {
192
192
currentTimerType = timerType ;
193
193
}
194
194
195
+ var lastMarkTimeStamp = null ;
196
+ var canUsePerformanceMeasure =
197
+ typeof performance !== 'undefined' &&
198
+ typeof performance . mark === 'function' &&
199
+ typeof performance . clearMarks === 'function' &&
200
+ typeof performance . measure === 'function' &&
201
+ typeof performance . clearMeasures === 'function' ;
202
+
203
+ function shouldMark ( debugID ) {
204
+ if ( ! isProfiling || ! canUsePerformanceMeasure ) {
205
+ return false ;
206
+ }
207
+ var element = ReactComponentTreeHook . getElement ( debugID ) ;
208
+ if ( element == null || typeof element !== 'object' ) {
209
+ return false ;
210
+ }
211
+ var isHostElement = typeof element . type === 'string' ;
212
+ if ( isHostElement ) {
213
+ return false ;
214
+ }
215
+ return true ;
216
+ }
217
+
218
+ function markBegin ( debugID , markType ) {
219
+ if ( ! shouldMark ( debugID ) ) {
220
+ return ;
221
+ }
222
+
223
+ var markName = `${ debugID } ::${ markType } ` ;
224
+ lastMarkTimeStamp = performanceNow ( ) ;
225
+ performance . mark ( markName ) ;
226
+ }
227
+
228
+ function markEnd ( debugID , markType ) {
229
+ if ( ! shouldMark ( debugID ) ) {
230
+ return ;
231
+ }
232
+
233
+ var markName = `${ debugID } ::${ markType } ` ;
234
+ var displayName = ReactComponentTreeHook . getDisplayName ( debugID ) ;
235
+
236
+ // Chrome has an issue of dropping markers recorded too fast:
237
+ // https://bugs.chromium.org/p/chromium/issues/detail?id=640652
238
+ // To work around this, we will not report very small measurements.
239
+ // I determined the magic number by tweaking it back and forth.
240
+ // 0.05ms was enough to prevent the issue, but I set it to 0.1ms to be safe.
241
+ // When the bug is fixed, we can `measure()` unconditionally if we want to.
242
+ var timeStamp = performanceNow ( ) ;
243
+ if ( timeStamp - lastMarkTimeStamp > 0.1 ) {
244
+ var measurementName = `${ displayName } [${ markType } ]` ;
245
+ performance . measure ( measurementName , markName ) ;
246
+ }
247
+
248
+ performance . clearMarks ( markName ) ;
249
+ performance . clearMeasures ( measurementName ) ;
250
+ }
251
+
195
252
var ReactDebugTool = {
196
253
addHook ( hook ) {
197
254
hooks . push ( hook ) ;
@@ -244,11 +301,13 @@ var ReactDebugTool = {
244
301
onBeginLifeCycleTimer ( debugID , timerType ) {
245
302
checkDebugID ( debugID ) ;
246
303
emitEvent ( 'onBeginLifeCycleTimer' , debugID , timerType ) ;
304
+ markBegin ( debugID , timerType ) ;
247
305
beginLifeCycleTimer ( debugID , timerType ) ;
248
306
} ,
249
307
onEndLifeCycleTimer ( debugID , timerType ) {
250
308
checkDebugID ( debugID ) ;
251
309
endLifeCycleTimer ( debugID , timerType ) ;
310
+ markEnd ( debugID , timerType ) ;
252
311
emitEvent ( 'onEndLifeCycleTimer' , debugID , timerType ) ;
253
312
} ,
254
313
onBeginProcessingChildContext ( ) {
@@ -273,25 +332,31 @@ var ReactDebugTool = {
273
332
checkDebugID ( debugID ) ;
274
333
checkDebugID ( parentDebugID , true ) ;
275
334
emitEvent ( 'onBeforeMountComponent' , debugID , element , parentDebugID ) ;
335
+ markBegin ( debugID , 'mount' ) ;
276
336
} ,
277
337
onMountComponent ( debugID ) {
278
338
checkDebugID ( debugID ) ;
339
+ markEnd ( debugID , 'mount' ) ;
279
340
emitEvent ( 'onMountComponent' , debugID ) ;
280
341
} ,
281
342
onBeforeUpdateComponent ( debugID , element ) {
282
343
checkDebugID ( debugID ) ;
283
344
emitEvent ( 'onBeforeUpdateComponent' , debugID , element ) ;
345
+ markBegin ( debugID , 'update' ) ;
284
346
} ,
285
347
onUpdateComponent ( debugID ) {
286
348
checkDebugID ( debugID ) ;
349
+ markEnd ( debugID , 'update' ) ;
287
350
emitEvent ( 'onUpdateComponent' , debugID ) ;
288
351
} ,
289
352
onBeforeUnmountComponent ( debugID ) {
290
353
checkDebugID ( debugID ) ;
291
354
emitEvent ( 'onBeforeUnmountComponent' , debugID ) ;
355
+ markBegin ( debugID , 'unmount' ) ;
292
356
} ,
293
357
onUnmountComponent ( debugID ) {
294
358
checkDebugID ( debugID ) ;
359
+ markEnd ( debugID , 'unmount' ) ;
295
360
emitEvent ( 'onUnmountComponent' , debugID ) ;
296
361
} ,
297
362
onTestEvent ( ) {
0 commit comments