Skip to content

Commit 9691ead

Browse files
committed
perf_hooks: reduce overhead of new performance_entries
1 parent 346abdd commit 9691ead

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

benchmark/perf_hooks/timerfied.js

Lines changed: 36 additions & 0 deletions
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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const {
44
ObjectDefineProperties,
5-
ReflectConstruct,
5+
ObjectSetPrototypeOf,
66
Symbol,
77
} = primordials;
88

@@ -91,10 +91,19 @@ function initPerformanceEntry(entry, name, type, start, duration) {
9191
entry[kDuration] = duration;
9292
}
9393

94+
function FastPerformanceEntry(name, type, start, duration) {
95+
initPerformanceEntry(this, name, type, start, duration);
96+
}
97+
98+
ObjectSetPrototypeOf(FastPerformanceEntry.prototype, PerformanceEntry.prototype);
99+
ObjectSetPrototypeOf(FastPerformanceEntry, PerformanceEntry);
100+
94101
function createPerformanceEntry(name, type, start, duration) {
95-
return ReflectConstruct(function PerformanceEntry() {
96-
initPerformanceEntry(this, name, type, start, duration);
97-
}, [], PerformanceEntry);
102+
const entry = new FastPerformanceEntry(name, type, start, duration);
103+
104+
entry.constructor = PerformanceEntry;
105+
106+
return entry;
98107
}
99108

100109
/**
@@ -118,11 +127,20 @@ class PerformanceNodeEntry extends PerformanceEntry {
118127
}
119128
}
120129

130+
function FastPerformanceNodeEntry(name, type, start, duration, detail) {
131+
initPerformanceEntry(this, name, type, start, duration);
132+
this[kDetail] = detail;
133+
}
134+
135+
ObjectSetPrototypeOf(FastPerformanceNodeEntry.prototype, PerformanceNodeEntry.prototype);
136+
ObjectSetPrototypeOf(FastPerformanceNodeEntry, PerformanceNodeEntry);
137+
121138
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);
139+
const entry = new FastPerformanceNodeEntry(name, type, start, duration, detail);
140+
141+
entry.constructor = PerformanceNodeEntry;
142+
143+
return entry;
126144
}
127145

128146
module.exports = {

0 commit comments

Comments
 (0)