Skip to content

Commit a439e79

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 a439e79

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

doc/api/perf_hooks.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ added: v8.5.0
4343
An object that can be used to collect performance metrics from the current
4444
Node.js instance. It is similar to [`window.performance`][] in browsers.
4545

46+
### `performance.toJSON()`
47+
<!-- YAML
48+
added: REPLACEME
49+
-->
50+
51+
An object which is JSON representation of the `performance` object. It
52+
is similar to [`window.performance.toJSON`][] in browsers.
53+
4654
### `performance.clearMarks([name])`
4755
<!-- YAML
4856
added: v8.5.0
@@ -1026,3 +1034,4 @@ require('some-module');
10261034
[`process.hrtime()`]: process.md#process_process_hrtime_time
10271035
[`timeOrigin`]: https://w3c.github.io/hr-time/#dom-performance-timeorigin
10281036
[`window.performance`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/performance
1037+
[`window.performance.toJSON`]: https://developer.mozilla.org/en-US/docs/Web/API/Performance/toJSON

lib/perf_hooks.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ class Performance extends EventTarget {
5959
timeOrigin: this.timeOrigin,
6060
}, opts)}`;
6161
}
62+
63+
toJSON() {
64+
return {
65+
nodeTiming: this.nodeTiming.toJSON(),
66+
timeOrigin: this.timeOrigin,
67+
eventLoopUtilization: this.eventLoopUtilization()
68+
};
69+
}
6270
}
6371

6472
class InternalPerformance extends EventTarget {}
Lines changed: 14 additions & 0 deletions
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)