@@ -95,11 +95,15 @@ const kSupportedEntryTypes = ObjectFreeze([
95
95
let markEntryBuffer = [ ] ;
96
96
let measureEntryBuffer = [ ] ;
97
97
let resourceTimingBuffer = [ ] ;
98
- const kMaxPerformanceEntryBuffers = 1e6 ;
98
+ const kPerformanceEntryBufferWarnSize = 1e6 ;
99
+ // https://www.w3.org/TR/timing-entrytypes-registry/#registry
100
+ // Default buffer limit for resource timing entries.
101
+ let resourceTimingBufferSizeLimit = 250 ;
102
+ let dispatchBufferFull ;
103
+
99
104
const kClearPerformanceEntryBuffers = ObjectFreeze ( {
100
105
'mark' : 'performance.clearMarks' ,
101
106
'measure' : 'performance.clearMeasures' ,
102
- 'resource' : 'performance.clearResourceTimings' ,
103
107
} ) ;
104
108
const kWarnedEntryTypes = new SafeMap ( ) ;
105
109
@@ -332,30 +336,38 @@ class PerformanceObserver {
332
336
}
333
337
}
334
338
339
+ /**
340
+ * https://www.w3.org/TR/performance-timeline/#dfn-queue-a-performanceentry
341
+ *
342
+ * Add the performance entry to the interested performance observer's queue.
343
+ */
335
344
function enqueue ( entry ) {
336
345
if ( ! isPerformanceEntry ( entry ) )
337
346
throw new ERR_INVALID_ARG_TYPE ( 'entry' , 'PerformanceEntry' , entry ) ;
338
347
339
348
for ( const obs of kObservers ) {
340
349
obs [ kMaybeBuffer ] ( entry ) ;
341
350
}
351
+ }
342
352
353
+ /**
354
+ * Add the user timing entry to the global buffer.
355
+ */
356
+ function bufferUserTiming ( entry ) {
343
357
const entryType = entry . entryType ;
344
358
let buffer ;
345
359
if ( entryType === 'mark' ) {
346
360
buffer = markEntryBuffer ;
347
361
} else if ( entryType === 'measure' ) {
348
362
buffer = measureEntryBuffer ;
349
- } else if ( entryType === 'resource' ) {
350
- buffer = resourceTimingBuffer ;
351
363
} else {
352
364
return ;
353
365
}
354
366
355
367
ArrayPrototypePush ( buffer , entry ) ;
356
368
const count = buffer . length ;
357
369
358
- if ( count > kMaxPerformanceEntryBuffers &&
370
+ if ( count > kPerformanceEntryBufferWarnSize &&
359
371
! kWarnedEntryTypes . has ( entryType ) ) {
360
372
kWarnedEntryTypes . set ( entryType , true ) ;
361
373
// No error code for this since it is a Warning
@@ -372,6 +384,32 @@ function enqueue(entry) {
372
384
}
373
385
}
374
386
387
+ /**
388
+ * Add the resource timing entry to the global buffer if the buffer size is not
389
+ * exceeding the buffer limit, or dispatch a buffer full event on the global
390
+ * performance object.
391
+ */
392
+ function bufferResourceTiming ( entry ) {
393
+ if ( resourceTimingBuffer . length >= resourceTimingBufferSizeLimit ) {
394
+ dispatchBufferFull ( 'resourcetimingbufferfull' ) ;
395
+ return ;
396
+ }
397
+
398
+ ArrayPrototypePush ( resourceTimingBuffer , entry ) ;
399
+ }
400
+
401
+ // https://w3c.github.io/resource-timing/#dom-performance-setresourcetimingbuffersize
402
+ function setResourceTimingBufferSize ( maxSize ) {
403
+ // If the maxSize parameter is less than resource timing buffer current
404
+ // size, no PerformanceResourceTiming objects are to be removed from the
405
+ // performance entry buffer.
406
+ resourceTimingBufferSizeLimit = maxSize ;
407
+ }
408
+
409
+ function setDispatchBufferFull ( fn ) {
410
+ dispatchBufferFull = fn ;
411
+ }
412
+
375
413
function clearEntriesFromBuffer ( type , name ) {
376
414
if ( type !== 'mark' && type !== 'measure' && type !== 'resource' ) {
377
415
return ;
@@ -492,4 +530,9 @@ module.exports = {
492
530
filterBufferMapByNameAndType,
493
531
startPerf,
494
532
stopPerf,
533
+
534
+ bufferUserTiming,
535
+ bufferResourceTiming,
536
+ setResourceTimingBufferSize,
537
+ setDispatchBufferFull,
495
538
} ;
0 commit comments