Skip to content

Commit 27a066c

Browse files
committed
perf: add toJSON to performance class
Added toJSON method to the InternalPerformance class as per the convention followed in other performance classes and per the spec: https://www.w3.org/TR/hr-time/#tojson-method Fixes: #37623
1 parent 8e8dea3 commit 27a066c

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

doc/api/perf_hooks.md

+9
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,14 @@ If the wrapped function returns a promise, a finally handler will be attached
261261
to the promise and the duration will be reported once the finally handler is
262262
invoked.
263263

264+
### `performance.toJSON()`
265+
<!-- YAML
266+
added: REPLACEME
267+
-->
268+
269+
An object which is JSON representation of the `performance` object. It
270+
is similar to [`window.performance.toJSON`][] in browsers.
271+
264272
## Class: `PerformanceEntry`
265273
<!-- YAML
266274
added: v8.5.0
@@ -1025,4 +1033,5 @@ require('some-module');
10251033
[`child_process.spawnSync()`]: child_process.md#child_process_child_process_spawnsync_command_args_options
10261034
[`process.hrtime()`]: process.md#process_process_hrtime_time
10271035
[`timeOrigin`]: https://w3c.github.io/hr-time/#dom-performance-timeorigin
1036+
[`window.performance.toJSON`]: https://developer.mozilla.org/en-US/docs/Web/API/Performance/toJSON
10281037
[`window.performance`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/performance

lib/perf_hooks.js

+12
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class Performance extends EventTarget {
5959
timeOrigin: this.timeOrigin,
6060
}, opts)}`;
6161
}
62+
6263
}
6364

6465
class InternalPerformance extends EventTarget {}
@@ -105,6 +106,17 @@ ObjectDefineProperties(Performance.prototype, {
105106
configurable: true,
106107
enumerable: true,
107108
value: timeOriginTimestamp,
109+
},
110+
toJSON: {
111+
configurable: true,
112+
enumerable: true,
113+
value: function toJSON() {
114+
return {
115+
nodeTiming: this.nodeTiming.toJSON(),
116+
timeOrigin: this.timeOrigin,
117+
eventLoopUtilization: this.eventLoopUtilization()
118+
};
119+
}
108120
}
109121
});
110122

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const { performance } = require('perf_hooks');
6+
7+
// Test toJSON for performance object
8+
{
9+
assert.strictEqual(typeof performance.toJSON, 'function');
10+
const jsonObject = performance.toJSON();
11+
assert.strictEqual(typeof jsonObject, 'object');
12+
assert.strictEqual(jsonObject.timeOrigin, performance.timeOrigin);
13+
assert.strictEqual(typeof jsonObject.nodeTiming, 'object');
14+
}

0 commit comments

Comments
 (0)