From 0568be863ba5e02bdbd3889f66281fae356f7263 Mon Sep 17 00:00:00 2001 From: Geert-Jan Zwiers Date: Fri, 13 May 2022 18:36:00 +0200 Subject: [PATCH] feat(ext/web): add performance.toJSON (#14548) --- cli/dts/lib.deno.shared_globals.d.ts | 4 ++++ cli/tests/unit/performance_test.ts | 9 +++++++++ ext/web/15_performance.js | 4 +++- tools/wpt/expectation.json | 2 -- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/cli/dts/lib.deno.shared_globals.d.ts b/cli/dts/lib.deno.shared_globals.d.ts index b96c56dba0..d430c16b07 100644 --- a/cli/dts/lib.deno.shared_globals.d.ts +++ b/cli/dts/lib.deno.shared_globals.d.ts @@ -450,6 +450,7 @@ declare class Worker extends EventTarget { declare type PerformanceEntryList = PerformanceEntry[]; declare class Performance { + /** Returns a timestamp representing the start of the performance measurement. */ readonly timeOrigin: number; constructor(); @@ -490,6 +491,9 @@ declare class Performance { * ``` */ now(): number; + + /** Returns a JSON representation of the performance object. */ + toJSON(): any; } declare var performance: Performance; diff --git a/cli/tests/unit/performance_test.ts b/cli/tests/unit/performance_test.ts index 252a31fc44..917546e2ca 100644 --- a/cli/tests/unit/performance_test.ts +++ b/cli/tests/unit/performance_test.ts @@ -27,6 +27,15 @@ Deno.test(function timeOrigin() { assert(Date.now() >= origin); }); +Deno.test(function performanceToJSON() { + const json = performance.toJSON(); + + assert("timeOrigin" in json); + assert(json.timeOrigin === performance.timeOrigin); + // check there are no other keys + assertEquals(Object.keys(json).length, 1); +}); + Deno.test(function performanceMark() { const mark = performance.mark("test"); assert(mark instanceof PerformanceMark); diff --git a/ext/web/15_performance.js b/ext/web/15_performance.js index d1437c8d7a..10e0140083 100644 --- a/ext/web/15_performance.js +++ b/ext/web/15_performance.js @@ -556,7 +556,9 @@ toJSON() { webidl.assertBranded(this, PerformancePrototype); - return {}; + return { + timeOrigin: this.timeOrigin, + }; } [customInspect](inspect) { diff --git a/tools/wpt/expectation.json b/tools/wpt/expectation.json index e71a48e2f3..2fa5f6e68e 100644 --- a/tools/wpt/expectation.json +++ b/tools/wpt/expectation.json @@ -1182,13 +1182,11 @@ "idlharness.any.html": [ "Performance interface: existence and properties of interface object", "Performance interface: existence and properties of interface prototype object", - "Performance interface: default toJSON operation on performance", "Window interface: attribute performance" ], "idlharness.any.worker.html": [ "Performance interface: existence and properties of interface object", "Performance interface: existence and properties of interface prototype object", - "Performance interface: default toJSON operation on performance", "WorkerGlobalScope interface: attribute performance", "WorkerGlobalScope interface: self must inherit property \"performance\" with the proper type" ],