Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf_hooks: reduce overhead of new resource timings #49837

Merged
merged 2 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
14 changes: 6 additions & 8 deletions lib/internal/perf/performance_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
}

class PerformanceEntry {
constructor(skipThrowSymbol = undefined) {
constructor(skipThrowSymbol = undefined, name = undefined, type = undefined, start = undefined, duration = undefined) {

Check failure on line 34 in lib/internal/perf/performance_entry.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

This line has a length of 121. Maximum allowed is 120
anonrig marked this conversation as resolved.
Show resolved Hide resolved
if (skipThrowSymbol !== kSkipThrow) {
throw new ERR_ILLEGAL_CONSTRUCTOR();
}

initPerformanceEntry(this, name, type, start, duration);
}

get name() {
Expand Down Expand Up @@ -94,11 +96,7 @@
}

function createPerformanceEntry(name, type, start, duration) {
const entry = new PerformanceEntry(kSkipThrow);

initPerformanceEntry(entry, name, type, start, duration);

return entry;
return new PerformanceEntry(kSkipThrow, name, type, start, duration);
}

/**
Expand All @@ -123,9 +121,8 @@
}

function createPerformanceNodeEntry(name, type, start, duration, detail) {
const entry = new PerformanceNodeEntry(kSkipThrow);
const entry = new PerformanceNodeEntry(kSkipThrow, name, type, start, duration);

initPerformanceEntry(entry, name, type, start, duration);
entry[kDetail] = detail;

return entry;
Expand All @@ -138,4 +135,5 @@
isPerformanceEntry,
PerformanceNodeEntry,
createPerformanceNodeEntry,
kSkipThrow,
};
38 changes: 20 additions & 18 deletions lib/internal/perf/resource_timing.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@

const {
ObjectDefineProperties,
ObjectSetPrototypeOf,
ReflectConstruct,
Symbol,
SymbolToStringTag,
} = primordials;
const { initPerformanceEntry, PerformanceEntry } = 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 { PerformanceEntry, kSkipThrow } = require('internal/perf/performance_entry');
const assert = require('internal/assert');
const { enqueue, bufferResourceTiming } = require('internal/perf/observe');
const { validateInternalField } = require('internal/validators');
const { kEnumerableProperty } = require('internal/util');

Expand All @@ -25,8 +23,12 @@ const kTimingInfo = Symbol('kTimingInfo');
const kInitiatorType = Symbol('kInitiatorType');

class PerformanceResourceTiming extends PerformanceEntry {
constructor() {
throw new ERR_ILLEGAL_CONSTRUCTOR();
constructor(skipThrowSymbol = undefined, name = undefined, type = undefined) {
if (skipThrowSymbol !== kSkipThrow) {
throw new ERR_ILLEGAL_CONSTRUCTOR();
}

super(skipThrowSymbol, name, type);
}

get name() {
Expand Down Expand Up @@ -189,16 +191,17 @@ 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, 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 +224,6 @@ function markResourceTiming(
cacheMode,
);

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