From 42d751149ac660b500e5de23316dd7d85211392b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Sat, 23 Sep 2023 22:25:44 -0300 Subject: [PATCH] perf_hooks: reduce overhead of new resource timings --- benchmark/perf_hooks/resourcetiming.js | 2 +- lib/internal/perf/performance_entry.js | 1 + lib/internal/perf/resource_timing.js | 36 +++++++++++--------------- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/benchmark/perf_hooks/resourcetiming.js b/benchmark/perf_hooks/resourcetiming.js index ddc40767b16196..51c4778d20065f 100644 --- a/benchmark/perf_hooks/resourcetiming.js +++ b/benchmark/perf_hooks/resourcetiming.js @@ -72,6 +72,6 @@ function main({ n, observe }) { obs.observe({ entryTypes: [observe], buffered: true }); bench.start(); - for (let i = 0; i < 1e5; i++) + for (let i = 0; i < n; i++) test(); } diff --git a/lib/internal/perf/performance_entry.js b/lib/internal/perf/performance_entry.js index aa97a652626606..57ccf3eb812afb 100644 --- a/lib/internal/perf/performance_entry.js +++ b/lib/internal/perf/performance_entry.js @@ -138,4 +138,5 @@ module.exports = { isPerformanceEntry, PerformanceNodeEntry, createPerformanceNodeEntry, + kSkipThrow, }; diff --git a/lib/internal/perf/resource_timing.js b/lib/internal/perf/resource_timing.js index 13f25d0af79cf7..10aa201e7d22ea 100644 --- a/lib/internal/perf/resource_timing.js +++ b/lib/internal/perf/resource_timing.js @@ -3,19 +3,12 @@ const { ObjectDefineProperties, - ObjectSetPrototypeOf, - ReflectConstruct, Symbol, SymbolToStringTag, } = primordials; -const { initPerformanceEntry, PerformanceEntry } = require('internal/perf/performance_entry'); +const { initPerformanceEntry, PerformanceEntry, kSkipThrow } = require('internal/perf/performance_entry'); const assert = require('internal/assert'); const { enqueue, bufferResourceTiming } = require('internal/perf/observe'); -const { - codes: { - ERR_ILLEGAL_CONSTRUCTOR, - }, -} = require('internal/errors'); const { validateInternalField } = require('internal/validators'); const { kEnumerableProperty } = require('internal/util'); @@ -25,8 +18,8 @@ const kTimingInfo = Symbol('kTimingInfo'); const kInitiatorType = Symbol('kInitiatorType'); class PerformanceResourceTiming extends PerformanceEntry { - constructor() { - throw new ERR_ILLEGAL_CONSTRUCTOR(); + constructor(skipThrowSymbol = undefined) { + super(skipThrowSymbol); } get name() { @@ -189,16 +182,18 @@ ObjectDefineProperties(PerformanceResourceTiming.prototype, { }); function createPerformanceResourceTiming(requestedUrl, initiatorType, timingInfo, cacheMode = '') { - return ReflectConstruct(function PerformanceResourceTiming() { - initPerformanceEntry(this, requestedUrl, 'resource'); - this[kInitiatorType] = initiatorType; - this[kRequestedUrl] = requestedUrl; - // https://fetch.spec.whatwg.org/#fetch-timing-info - // This class is using timingInfo assuming it's already validated. - // The spec doesn't say to validate it in the class construction. - this[kTimingInfo] = timingInfo; - this[kCacheMode] = cacheMode; - }, [], PerformanceResourceTiming); + const resourceTiming = new PerformanceResourceTiming(kSkipThrow); + + initPerformanceEntry(resourceTiming, requestedUrl, 'resource'); + resourceTiming[kInitiatorType] = initiatorType; + resourceTiming[kRequestedUrl] = requestedUrl; + // https://fetch.spec.whatwg.org/#fetch-timing-info + // This class is using timingInfo assuming it's already validated. + // The spec doesn't say to validate it in the class construction. + resourceTiming[kTimingInfo] = timingInfo; + resourceTiming[kCacheMode] = cacheMode; + + return resourceTiming; } // https://w3c.github.io/resource-timing/#dfn-mark-resource-timing @@ -221,7 +216,6 @@ function markResourceTiming( cacheMode, ); - ObjectSetPrototypeOf(resource, PerformanceResourceTiming.prototype); enqueue(resource); bufferResourceTiming(resource); return resource;