Skip to content

Commit 0e507d3

Browse files
authored
perf_hooks: reduce overhead of new user timings
PR-URL: #49914 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Stephen Belanger <[email protected]>
1 parent 1d220b5 commit 0e507d3

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

lib/internal/perf/performance_entry.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ class PerformanceEntry {
4242
throw new ERR_ILLEGAL_CONSTRUCTOR();
4343
}
4444

45-
initPerformanceEntry(this, name, type, start, duration);
45+
this[kName] = name;
46+
this[kEntryType] = type;
47+
this[kStartTime] = start;
48+
this[kDuration] = duration;
4649
}
4750

4851
get name() {
@@ -94,13 +97,6 @@ ObjectDefineProperties(PerformanceEntry.prototype, {
9497
toJSON: kEnumerableProperty,
9598
});
9699

97-
function initPerformanceEntry(entry, name, type, start, duration) {
98-
entry[kName] = name;
99-
entry[kEntryType] = type;
100-
entry[kStartTime] = start;
101-
entry[kDuration] = duration;
102-
}
103-
104100
function createPerformanceEntry(name, type, start, duration) {
105101
return new PerformanceEntry(kSkipThrow, name, type, start, duration);
106102
}
@@ -135,7 +131,6 @@ function createPerformanceNodeEntry(name, type, start, duration, detail) {
135131
}
136132

137133
module.exports = {
138-
initPerformanceEntry,
139134
createPerformanceEntry,
140135
PerformanceEntry,
141136
isPerformanceEntry,

lib/internal/perf/usertiming.js

+28-19
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
const {
44
ObjectDefineProperties,
5-
ObjectSetPrototypeOf,
65
SafeMap,
76
SafeSet,
87
SafeArrayIterator,
98
Symbol,
109
SymbolToStringTag,
11-
ReflectConstruct,
1210
} = primordials;
1311

14-
const { initPerformanceEntry, PerformanceEntry } = require('internal/perf/performance_entry');
12+
const { PerformanceEntry, kSkipThrow } = require('internal/perf/performance_entry');
1513
const { now } = require('internal/perf/utils');
1614
const { enqueue, bufferUserTiming } = require('internal/perf/observe');
1715
const nodeTiming = require('internal/perf/nodetiming');
@@ -35,7 +33,6 @@ const {
3533

3634
const { structuredClone } = require('internal/structured_clone');
3735
const {
38-
kEmptyObject,
3936
lazyDOMException,
4037
kEnumerableProperty,
4138
} = require('internal/util');
@@ -69,27 +66,29 @@ function getMark(name) {
6966
return ts;
7067
}
7168

72-
class PerformanceMark {
73-
constructor(name, options = kEmptyObject) {
69+
class PerformanceMark extends PerformanceEntry {
70+
constructor(name, options = undefined) {
7471
if (arguments.length === 0) {
7572
throw new ERR_MISSING_ARGS('name');
7673
}
7774
name = `${name}`;
78-
options ??= kEmptyObject;
7975
if (nodeTimingReadOnlyAttributes.has(name))
8076
throw new ERR_INVALID_ARG_VALUE('name', name);
81-
validateObject(options, 'options');
82-
const startTime = options.startTime ?? now();
77+
if (options != null) {
78+
validateObject(options, 'options');
79+
}
80+
const startTime = options?.startTime ?? now();
8381
validateNumber(startTime, 'startTime');
8482
if (startTime < 0)
8583
throw new ERR_PERFORMANCE_INVALID_TIMESTAMP(startTime);
8684
markTimings.set(name, startTime);
8785

88-
let detail = options.detail;
86+
let detail = options?.detail;
8987
detail = detail != null ?
9088
structuredClone(detail) :
9189
null;
92-
initPerformanceEntry(this, name, 'mark', startTime, 0);
90+
91+
super(kSkipThrow, name, 'mark', startTime, 0);
9392
this[kDetail] = detail;
9493
}
9594

@@ -108,8 +107,7 @@ class PerformanceMark {
108107
};
109108
}
110109
}
111-
ObjectSetPrototypeOf(PerformanceMark, PerformanceEntry);
112-
ObjectSetPrototypeOf(PerformanceMark.prototype, PerformanceEntry.prototype);
110+
113111
ObjectDefineProperties(PerformanceMark.prototype, {
114112
detail: kEnumerableProperty,
115113
[SymbolToStringTag]: {
@@ -120,8 +118,18 @@ ObjectDefineProperties(PerformanceMark.prototype, {
120118
});
121119

122120
class PerformanceMeasure extends PerformanceEntry {
123-
constructor() {
124-
throw new ERR_ILLEGAL_CONSTRUCTOR();
121+
constructor(
122+
skipThrowSymbol = undefined,
123+
name = undefined,
124+
type = undefined,
125+
start = undefined,
126+
duration = undefined,
127+
) {
128+
if (skipThrowSymbol !== kSkipThrow) {
129+
throw new ERR_ILLEGAL_CONSTRUCTOR();
130+
}
131+
132+
super(skipThrowSymbol, name, type, start, duration);
125133
}
126134

127135
get detail() {
@@ -139,10 +147,11 @@ ObjectDefineProperties(PerformanceMeasure.prototype, {
139147
});
140148

141149
function createPerformanceMeasure(name, start, duration, detail) {
142-
return ReflectConstruct(function PerformanceMeasure() {
143-
initPerformanceEntry(this, name, 'measure', start, duration);
144-
this[kDetail] = detail;
145-
}, [], PerformanceMeasure);
150+
const measure = new PerformanceMeasure(kSkipThrow, name, 'measure', start, duration);
151+
152+
measure[kDetail] = detail;
153+
154+
return measure;
146155
}
147156

148157
function mark(name, options) {

0 commit comments

Comments
 (0)