Skip to content

Commit f40b5ed

Browse files
H4adruyadorno
authored andcommitted
perf_hooks: reduce overhead of new performance_entries
PR-URL: #49803 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent d44a812 commit f40b5ed

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

benchmark/perf_hooks/timerfied.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const common = require('../common.js');
5+
6+
const {
7+
PerformanceObserver,
8+
performance,
9+
} = require('perf_hooks');
10+
11+
function randomFn() {
12+
return Math.random();
13+
}
14+
15+
const bench = common.createBenchmark(main, {
16+
n: [1e5],
17+
observe: ['function'],
18+
});
19+
20+
let _result;
21+
22+
function main({ n, observe }) {
23+
const obs = new PerformanceObserver(() => {
24+
bench.end(n);
25+
});
26+
obs.observe({ entryTypes: [observe], buffered: true });
27+
28+
const timerfied = performance.timerify(randomFn);
29+
30+
bench.start();
31+
for (let i = 0; i < n; i++)
32+
_result = timerfied();
33+
34+
// Avoid V8 deadcode (elimination)
35+
assert.ok(_result);
36+
}

lib/internal/perf/performance_entry.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const {
44
ObjectDefineProperties,
5-
ReflectConstruct,
65
Symbol,
76
} = primordials;
87

@@ -25,14 +24,17 @@ const kEntryType = Symbol('PerformanceEntry.EntryType');
2524
const kStartTime = Symbol('PerformanceEntry.StartTime');
2625
const kDuration = Symbol('PerformanceEntry.Duration');
2726
const kDetail = Symbol('NodePerformanceEntry.Detail');
27+
const kSkipThrow = Symbol('kSkipThrow');
2828

2929
function isPerformanceEntry(obj) {
3030
return obj?.[kName] !== undefined;
3131
}
3232

3333
class PerformanceEntry {
34-
constructor() {
35-
throw new ERR_ILLEGAL_CONSTRUCTOR();
34+
constructor(skipThrowSymbol = undefined) {
35+
if (skipThrowSymbol !== kSkipThrow) {
36+
throw new ERR_ILLEGAL_CONSTRUCTOR();
37+
}
3638
}
3739

3840
get name() {
@@ -92,9 +94,11 @@ function initPerformanceEntry(entry, name, type, start, duration) {
9294
}
9395

9496
function createPerformanceEntry(name, type, start, duration) {
95-
return ReflectConstruct(function PerformanceEntry() {
96-
initPerformanceEntry(this, name, type, start, duration);
97-
}, [], PerformanceEntry);
97+
const entry = new PerformanceEntry(kSkipThrow);
98+
99+
initPerformanceEntry(entry, name, type, start, duration);
100+
101+
return entry;
98102
}
99103

100104
/**
@@ -119,10 +123,12 @@ class PerformanceNodeEntry extends PerformanceEntry {
119123
}
120124

121125
function createPerformanceNodeEntry(name, type, start, duration, detail) {
122-
return ReflectConstruct(function PerformanceNodeEntry() {
123-
initPerformanceEntry(this, name, type, start, duration);
124-
this[kDetail] = detail;
125-
}, [], PerformanceNodeEntry);
126+
const entry = new PerformanceNodeEntry(kSkipThrow);
127+
128+
initPerformanceEntry(entry, name, type, start, duration);
129+
entry[kDetail] = detail;
130+
131+
return entry;
126132
}
127133

128134
module.exports = {

0 commit comments

Comments
 (0)