Skip to content

Commit 9daa856

Browse files
authored
docs(utils): clarify buffered mode implementation in PerformanceObserverSink (#1233)
1 parent 243e105 commit 9daa856

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

packages/utils/src/lib/performance-observer.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ export type PerformanceObserverOptions<T> = {
9595

9696
/**
9797
* Whether to enable buffered observation mode.
98-
* When true, captures all performance entries that occurred before observation started.
99-
* When false, only captures entries after subscription begins.
98+
*
99+
* When true, captures all performance marks and measures that exist in the Node.js
100+
* performance buffer at the time `subscribe()` is called using `performance.getEntriesByType()`
101+
* (the native `buffered` option is unreliable in Node.js).
100102
*
101103
* @default true
102104
*/
@@ -309,9 +311,8 @@ export class PerformanceObserverSink<T> {
309311
*
310312
* Creates a Node.js PerformanceObserver that monitors 'mark' and 'measure' entries.
311313
* The observer uses a bounded queue with proactive flushing to manage memory usage.
312-
* When buffered mode is enabled, any existing buffered entries are immediately flushed.
314+
* When buffered mode is enabled, existing entries are captured via `performance.getEntriesByType()` instead of the unreliable native `buffered` option.
313315
* If the sink is closed, items stay in the queue until reopened.
314-
*
315316
*/
316317
subscribe(): void {
317318
if (this.#observer) {
@@ -322,11 +323,7 @@ export class PerformanceObserverSink<T> {
322323
this.processPerformanceEntries(list.getEntries());
323324
});
324325

325-
// When buffered mode is enabled, Node.js PerformanceObserver invokes
326-
// the callback synchronously with all buffered entries before observe() returns.
327-
// However, entries created before any observer existed may not be buffered by Node.js.
328-
// We manually retrieve entries from the performance buffer using getEntriesByType()
329-
// to capture entries that were created before the observer was created.
326+
// Manually capture buffered entries instead of the unreliable native buffered option.
330327
if (this.#buffered) {
331328
const existingMarks = performance.getEntriesByType('mark');
332329
const existingMeasures = performance.getEntriesByType('measure');
@@ -336,8 +333,7 @@ export class PerformanceObserverSink<T> {
336333

337334
this.#observer.observe({
338335
entryTypes: OBSERVED_TYPES,
339-
// @NOTE: This is for unknown reasons not working, and we manually do it above
340-
// buffered: this.#buffered,
336+
// Note: buffered option intentionally omitted due to unreliability in Node.js.
341337
});
342338
}
343339

0 commit comments

Comments
 (0)