Skip to content

Commit 5647f64

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 5647f64

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/perf_hooks.js

Lines changed: 9 additions & 1 deletion
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 {}
@@ -114,7 +122,7 @@ module.exports = {
114122
PerformanceObserver,
115123
monitorEventLoopDelay,
116124
createHistogram,
117-
performance: new InternalPerformance(),
125+
performance: new InternalPerformance()
118126
};
119127

120128
ObjectDefineProperty(module.exports, 'constants', {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const { performance } = require('perf_hooks');
5+
6+
// Test toJSON for performance object
7+
{
8+
assert.strictEqual(typeof performance.toJSON, 'function');
9+
const jsonObject = performance.toJSON();
10+
assert.strictEqual(typeof jsonObject, 'object');
11+
assert.strictEqual(jsonObject.timeOrigin, performance.timeOrigin);
12+
assert.strictEqual(typeof jsonObject.nodeTiming, 'object');
13+
}

0 commit comments

Comments
 (0)