lib: add toJSON to PerformanceMeasure

PR-URL: https://github.com/nodejs/node/pull/53603
Refs: https://github.com/nodejs/node/issues/53570
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
theanarkh 2024-06-29 12:48:23 +08:00 committed by GitHub
parent d65b17082b
commit 77710251e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 0 deletions

View File

@ -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,

View File

@ -0,0 +1,20 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const util = require('util');
const { performance, PerformanceObserver } = require('perf_hooks');
const perfObserver = new PerformanceObserver(common.mustCall((items) => {
const entries = items.getEntries();
assert.ok(entries.length === 1);
for (const entry of entries) {
assert.ok(util.inspect(entry).includes('this is detail'));
}
}));
perfObserver.observe({ entryTypes: ['measure'] });
performance.measure('sample', {
detail: 'this is detail',
});