Skip to content

Commit f39ee90

Browse files
juanarboltargos
authored andcommitted
doc: improve perf_hooks docs
PR-URL: #36909 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent d13fc6e commit f39ee90

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

doc/api/perf_hooks.md

+104
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,38 @@ added: v8.5.0
521521
Returns a list of `PerformanceEntry` objects in chronological order
522522
with respect to `performanceEntry.startTime`.
523523

524+
```js
525+
const {
526+
performance,
527+
PerformanceObserver
528+
} = require('perf_hooks');
529+
530+
const obs = new PerformanceObserver((perfObserverList, observer) => {
531+
console.log(perfObserverList.getEntries());
532+
/**
533+
* [
534+
* PerformanceEntry {
535+
* name: 'test',
536+
* entryType: 'mark',
537+
* startTime: 81.465639,
538+
* duration: 0
539+
* },
540+
* PerformanceEntry {
541+
* name: 'meow',
542+
* entryType: 'mark',
543+
* startTime: 81.860064,
544+
* duration: 0
545+
* }
546+
* ]
547+
*/
548+
observer.disconnect();
549+
});
550+
obs.observe({ entryTypes: ['mark'], buffered: true });
551+
552+
performance.mark('test');
553+
performance.mark('meow');
554+
```
555+
524556
### `performanceObserverEntryList.getEntriesByName(name[, type])`
525557
<!-- YAML
526558
added: v8.5.0
@@ -535,6 +567,46 @@ with respect to `performanceEntry.startTime` whose `performanceEntry.name` is
535567
equal to `name`, and optionally, whose `performanceEntry.entryType` is equal to
536568
`type`.
537569

570+
```js
571+
const {
572+
performance,
573+
PerformanceObserver
574+
} = require('perf_hooks');
575+
576+
const obs = new PerformanceObserver((perfObserverList, observer) => {
577+
console.log(perfObserverList.getEntriesByName('meow'));
578+
/**
579+
* [
580+
* PerformanceEntry {
581+
* name: 'meow',
582+
* entryType: 'mark',
583+
* startTime: 98.545991,
584+
* duration: 0
585+
* }
586+
* ]
587+
*/
588+
console.log(perfObserverList.getEntriesByName('nope')); // []
589+
590+
console.log(perfObserverList.getEntriesByName('test', 'mark'));
591+
/**
592+
* [
593+
* PerformanceEntry {
594+
* name: 'test',
595+
* entryType: 'mark',
596+
* startTime: 63.518931,
597+
* duration: 0
598+
* }
599+
* ]
600+
*/
601+
console.log(perfObserverList.getEntriesByName('test', 'measure')); // []
602+
observer.disconnect();
603+
});
604+
obs.observe({ entryTypes: ['mark', 'measure'], buffered: true });
605+
606+
performance.mark('test');
607+
performance.mark('meow');
608+
```
609+
538610
### `performanceObserverEntryList.getEntriesByType(type)`
539611
<!-- YAML
540612
added: v8.5.0
@@ -547,6 +619,38 @@ Returns a list of `PerformanceEntry` objects in chronological order
547619
with respect to `performanceEntry.startTime` whose `performanceEntry.entryType`
548620
is equal to `type`.
549621

622+
```js
623+
const {
624+
performance,
625+
PerformanceObserver
626+
} = require('perf_hooks');
627+
628+
const obs = new PerformanceObserver((perfObserverList, observer) => {
629+
console.log(perfObserverList.getEntriesByType('mark'));
630+
/**
631+
* [
632+
* PerformanceEntry {
633+
* name: 'test',
634+
* entryType: 'mark',
635+
* startTime: 55.897834,
636+
* duration: 0
637+
* },
638+
* PerformanceEntry {
639+
* name: 'meow',
640+
* entryType: 'mark',
641+
* startTime: 56.350146,
642+
* duration: 0
643+
* }
644+
* ]
645+
*/
646+
observer.disconnect();
647+
});
648+
obs.observe({ entryTypes: ['mark'], buffered: true });
649+
650+
performance.mark('test');
651+
performance.mark('meow');
652+
```
653+
550654
## `perf_hooks.monitorEventLoopDelay([options])`
551655
<!-- YAML
552656
added: v11.10.0

0 commit comments

Comments
 (0)