From 7bd3cae26b1a6a168753d46406a90633b79ee54f Mon Sep 17 00:00:00 2001 From: theanarkh Date: Thu, 27 Jun 2024 09:25:14 +0800 Subject: [PATCH] lib: add toJSON to PerformanceMeasure --- lib/internal/perf/usertiming.js | 10 ++++++++++ .../test-performance-measure-detail.js | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 test/parallel/test-performance-measure-detail.js diff --git a/lib/internal/perf/usertiming.js b/lib/internal/perf/usertiming.js index ab109c3c04bce6..dfbbcbaf36c538 100644 --- a/lib/internal/perf/usertiming.js +++ b/lib/internal/perf/usertiming.js @@ -136,6 +136,16 @@ class PerformanceMeasure extends PerformanceEntry { validateInternalField(this, kDetail, 'PerformanceMeasure'); return this[kDetail]; } + + toJSON() { + return { + name: this.name, + entryType: this.entryType, + startTime: this.startTime, + duration: this.duration, + detail: this[kDetail], + }; + } } ObjectDefineProperties(PerformanceMeasure.prototype, { detail: kEnumerableProperty, diff --git a/test/parallel/test-performance-measure-detail.js b/test/parallel/test-performance-measure-detail.js new file mode 100644 index 00000000000000..e0540e3259594c --- /dev/null +++ b/test/parallel/test-performance-measure-detail.js @@ -0,0 +1,18 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); +const util = require('util'); +const { performance, PerformanceObserver } = require('perf_hooks'); + +const perfObserver = new PerformanceObserver((items) => { + items.getEntries().forEach((entry) => { + assert.ok(util.inspect(entry).includes('this is detail')); + }); +}); + +perfObserver.observe({ entryTypes: ['measure'] }); + +performance.measure('sample', { + detail: 'this is detail', +});