Skip to content

Commit

Permalink
perf_hooks: reduce overhead of new resource timings
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Sep 24, 2023
1 parent 36627f2 commit 42d7511
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
2 changes: 1 addition & 1 deletion benchmark/perf_hooks/resourcetiming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
1 change: 1 addition & 0 deletions lib/internal/perf/performance_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,5 @@ module.exports = {
isPerformanceEntry,
PerformanceNodeEntry,
createPerformanceNodeEntry,
kSkipThrow,
};
36 changes: 15 additions & 21 deletions lib/internal/perf/resource_timing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -221,7 +216,6 @@ function markResourceTiming(
cacheMode,
);

ObjectSetPrototypeOf(resource, PerformanceResourceTiming.prototype);
enqueue(resource);
bufferResourceTiming(resource);
return resource;
Expand Down

0 comments on commit 42d7511

Please sign in to comment.