Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 06221a7

Browse files
committedJun 6, 2022
pref_hooks: fix function wrapped by timerify to work correctly
1 parent dfa896f commit 06221a7

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed
 

‎lib/internal/perf/timerify.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ const {
2020
isHistogram
2121
} = require('internal/histogram');
2222

23-
const {
24-
isConstructor,
25-
} = internalBinding('util');
26-
2723
const {
2824
codes: {
2925
ERR_INVALID_ARG_TYPE,
@@ -68,14 +64,13 @@ function timerify(fn, options = {}) {
6864
histogram);
6965
}
7066

71-
const constructor = isConstructor(fn);
72-
7367
function timerified(...args) {
68+
const isConstructorCall = !!new.target;
7469
const start = now();
75-
const result = constructor ?
70+
const result = isConstructorCall ?
7671
ReflectConstruct(fn, args, fn) :
7772
ReflectApply(fn, this, args);
78-
if (!constructor && typeof result?.finally === 'function') {
73+
if (!isConstructorCall && typeof result?.finally === 'function') {
7974
return result.finally(
8075
FunctionPrototypeBind(
8176
processComplete,

‎test/parallel/test-performance-function.js

+19
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,22 @@ const {
123123
});
124124
});
125125
})().then(common.mustCall());
126+
127+
// Regression tests for https://github.com/nodejs/node/issues/40623
128+
{
129+
assert.strictEqual(performance.timerify(function func() {
130+
return 1;
131+
})(), 1);
132+
assert.strictEqual(performance.timerify(function() {
133+
return 1;
134+
})(), 1);
135+
assert.strictEqual(performance.timerify(() => {
136+
return 1;
137+
})(), 1);
138+
class C {}
139+
const wrap = performance.timerify(C);
140+
assert.ok(new wrap() instanceof C);
141+
assert.throws(() => wrap(), {
142+
name: 'TypeError',
143+
});
144+
}

0 commit comments

Comments
 (0)
Please sign in to comment.