Skip to content

Commit e6e320e

Browse files
authored
perf_hooks: reduce overhead of new resource timings
PR-URL: #49837 Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent 0ee9c83 commit e6e320e

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

benchmark/perf_hooks/resourcetiming.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ function main({ n, observe }) {
7272
obs.observe({ entryTypes: [observe], buffered: true });
7373

7474
bench.start();
75-
for (let i = 0; i < 1e5; i++)
75+
for (let i = 0; i < n; i++)
7676
test();
7777
}

lib/internal/perf/performance_entry.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,18 @@ function isPerformanceEntry(obj) {
3131
}
3232

3333
class PerformanceEntry {
34-
constructor(skipThrowSymbol = undefined) {
34+
constructor(
35+
skipThrowSymbol = undefined,
36+
name = undefined,
37+
type = undefined,
38+
start = undefined,
39+
duration = undefined,
40+
) {
3541
if (skipThrowSymbol !== kSkipThrow) {
3642
throw new ERR_ILLEGAL_CONSTRUCTOR();
3743
}
44+
45+
initPerformanceEntry(this, name, type, start, duration);
3846
}
3947

4048
get name() {
@@ -94,11 +102,7 @@ function initPerformanceEntry(entry, name, type, start, duration) {
94102
}
95103

96104
function createPerformanceEntry(name, type, start, duration) {
97-
const entry = new PerformanceEntry(kSkipThrow);
98-
99-
initPerformanceEntry(entry, name, type, start, duration);
100-
101-
return entry;
105+
return new PerformanceEntry(kSkipThrow, name, type, start, duration);
102106
}
103107

104108
/**
@@ -123,9 +127,8 @@ class PerformanceNodeEntry extends PerformanceEntry {
123127
}
124128

125129
function createPerformanceNodeEntry(name, type, start, duration, detail) {
126-
const entry = new PerformanceNodeEntry(kSkipThrow);
130+
const entry = new PerformanceNodeEntry(kSkipThrow, name, type, start, duration);
127131

128-
initPerformanceEntry(entry, name, type, start, duration);
129132
entry[kDetail] = detail;
130133

131134
return entry;
@@ -138,4 +141,5 @@ module.exports = {
138141
isPerformanceEntry,
139142
PerformanceNodeEntry,
140143
createPerformanceNodeEntry,
144+
kSkipThrow,
141145
};

lib/internal/perf/resource_timing.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33

44
const {
55
ObjectDefineProperties,
6-
ObjectSetPrototypeOf,
7-
ReflectConstruct,
86
Symbol,
97
SymbolToStringTag,
108
} = primordials;
11-
const { initPerformanceEntry, PerformanceEntry } = require('internal/perf/performance_entry');
12-
const assert = require('internal/assert');
13-
const { enqueue, bufferResourceTiming } = require('internal/perf/observe');
149
const {
1510
codes: {
1611
ERR_ILLEGAL_CONSTRUCTOR,
1712
},
1813
} = require('internal/errors');
14+
const { PerformanceEntry, kSkipThrow } = require('internal/perf/performance_entry');
15+
const assert = require('internal/assert');
16+
const { enqueue, bufferResourceTiming } = require('internal/perf/observe');
1917
const { validateInternalField } = require('internal/validators');
2018
const { kEnumerableProperty } = require('internal/util');
2119

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

2725
class PerformanceResourceTiming extends PerformanceEntry {
28-
constructor() {
29-
throw new ERR_ILLEGAL_CONSTRUCTOR();
26+
constructor(skipThrowSymbol = undefined, name = undefined, type = undefined) {
27+
if (skipThrowSymbol !== kSkipThrow) {
28+
throw new ERR_ILLEGAL_CONSTRUCTOR();
29+
}
30+
31+
super(skipThrowSymbol, name, type);
3032
}
3133

3234
get name() {
@@ -189,16 +191,17 @@ ObjectDefineProperties(PerformanceResourceTiming.prototype, {
189191
});
190192

191193
function createPerformanceResourceTiming(requestedUrl, initiatorType, timingInfo, cacheMode = '') {
192-
return ReflectConstruct(function PerformanceResourceTiming() {
193-
initPerformanceEntry(this, requestedUrl, 'resource');
194-
this[kInitiatorType] = initiatorType;
195-
this[kRequestedUrl] = requestedUrl;
196-
// https://fetch.spec.whatwg.org/#fetch-timing-info
197-
// This class is using timingInfo assuming it's already validated.
198-
// The spec doesn't say to validate it in the class construction.
199-
this[kTimingInfo] = timingInfo;
200-
this[kCacheMode] = cacheMode;
201-
}, [], PerformanceResourceTiming);
194+
const resourceTiming = new PerformanceResourceTiming(kSkipThrow, requestedUrl, 'resource');
195+
196+
resourceTiming[kInitiatorType] = initiatorType;
197+
resourceTiming[kRequestedUrl] = requestedUrl;
198+
// https://fetch.spec.whatwg.org/#fetch-timing-info
199+
// This class is using timingInfo assuming it's already validated.
200+
// The spec doesn't say to validate it in the class construction.
201+
resourceTiming[kTimingInfo] = timingInfo;
202+
resourceTiming[kCacheMode] = cacheMode;
203+
204+
return resourceTiming;
202205
}
203206

204207
// https://w3c.github.io/resource-timing/#dfn-mark-resource-timing
@@ -221,7 +224,6 @@ function markResourceTiming(
221224
cacheMode,
222225
);
223226

224-
ObjectSetPrototypeOf(resource, PerformanceResourceTiming.prototype);
225227
enqueue(resource);
226228
bufferResourceTiming(resource);
227229
return resource;

0 commit comments

Comments
 (0)