Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: add toJSON to performance class #37771

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/api/perf_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ If the wrapped function returns a promise, a finally handler will be attached
to the promise and the duration will be reported once the finally handler is
invoked.

### `performance.toJSON()`
<!-- YAML
added: REPLACEME
-->

An object which is JSON representation of the `performance` object. It
is similar to [`window.performance.toJSON`][] in browsers.

## Class: `PerformanceEntry`
<!-- YAML
added: v8.5.0
Expand Down Expand Up @@ -1025,4 +1033,5 @@ require('some-module');
[`child_process.spawnSync()`]: child_process.md#child_process_child_process_spawnsync_command_args_options
[`process.hrtime()`]: process.md#process_process_hrtime_time
[`timeOrigin`]: https://w3c.github.io/hr-time/#dom-performance-timeorigin
[`window.performance.toJSON`]: https://developer.mozilla.org/en-US/docs/Web/API/Performance/toJSON
[`window.performance`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/performance
14 changes: 14 additions & 0 deletions lib/perf_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ class Performance extends EventTarget {
timeOrigin: this.timeOrigin,
}, opts)}`;
}

}

function toJSON() {
return {
nodeTiming: this.nodeTiming,
timeOrigin: this.timeOrigin,
eventLoopUtilization: this.eventLoopUtilization()
jasnell marked this conversation as resolved.
Show resolved Hide resolved
};
}

class InternalPerformance extends EventTarget {}
Expand Down Expand Up @@ -105,6 +114,11 @@ ObjectDefineProperties(Performance.prototype, {
configurable: true,
enumerable: true,
value: timeOriginTimestamp,
},
toJSON: {
configurable: true,
enumerable: true,
value: toJSON,
}
});

Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-tojson-perf_hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

require('../common');
const assert = require('assert');
const { performance } = require('perf_hooks');

// Test toJSON for performance object
{
assert.strictEqual(typeof performance.toJSON, 'function');
const jsonObject = performance.toJSON();
assert.strictEqual(typeof jsonObject, 'object');
assert.strictEqual(jsonObject.timeOrigin, performance.timeOrigin);
assert.strictEqual(typeof jsonObject.nodeTiming, 'object');
}