Skip to content

Commit

Permalink
perf: add toJSON to performance class
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yashLadha committed Mar 17, 2021
1 parent 8e8dea3 commit 5647f64
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/perf_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ class Performance extends EventTarget {
timeOrigin: this.timeOrigin,
}, opts)}`;
}

toJSON() {
return {
nodeTiming: this.nodeTiming.toJSON(),
timeOrigin: this.timeOrigin,
eventLoopUtilization: this.eventLoopUtilization()
};
}
}

class InternalPerformance extends EventTarget {}
Expand Down Expand Up @@ -114,7 +122,7 @@ module.exports = {
PerformanceObserver,
monitorEventLoopDelay,
createHistogram,
performance: new InternalPerformance(),
performance: new InternalPerformance()
};

ObjectDefineProperty(module.exports, 'constants', {
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-tojson-perf_hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

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');
}

0 comments on commit 5647f64

Please sign in to comment.