Skip to content

Commit 4711f57

Browse files
yashLadhatargos
authored andcommitted
perf_hooks: 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 PR-URL: #37771 Fixes: #37623 Reviewed-By: James M Snell <[email protected]>
1 parent 9258604 commit 4711f57

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-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

+14
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ class Performance extends EventTarget {
5959
timeOrigin: this.timeOrigin,
6060
}, opts)}`;
6161
}
62+
63+
}
64+
65+
function toJSON() {
66+
return {
67+
nodeTiming: this.nodeTiming,
68+
timeOrigin: this.timeOrigin,
69+
eventLoopUtilization: this.eventLoopUtilization()
70+
};
6271
}
6372

6473
class InternalPerformance extends EventTarget {}
@@ -105,6 +114,11 @@ ObjectDefineProperties(Performance.prototype, {
105114
configurable: true,
106115
enumerable: true,
107116
value: timeOriginTimestamp,
117+
},
118+
toJSON: {
119+
configurable: true,
120+
enumerable: true,
121+
value: toJSON,
108122
}
109123
});
110124

+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)