Skip to content

Commit

Permalink
pref_hooks: fix function wrapped by timerify to work correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
cola119 committed Jun 6, 2022
1 parent dfa896f commit 06221a7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
11 changes: 3 additions & 8 deletions lib/internal/perf/timerify.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ const {
isHistogram
} = require('internal/histogram');

const {
isConstructor,
} = internalBinding('util');

const {
codes: {
ERR_INVALID_ARG_TYPE,
Expand Down Expand Up @@ -68,14 +64,13 @@ function timerify(fn, options = {}) {
histogram);
}

const constructor = isConstructor(fn);

function timerified(...args) {
const isConstructorCall = !!new.target;
const start = now();
const result = constructor ?
const result = isConstructorCall ?
ReflectConstruct(fn, args, fn) :
ReflectApply(fn, this, args);
if (!constructor && typeof result?.finally === 'function') {
if (!isConstructorCall && typeof result?.finally === 'function') {
return result.finally(
FunctionPrototypeBind(
processComplete,
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-performance-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,22 @@ const {
});
});
})().then(common.mustCall());

// Regression tests for https://github.com/nodejs/node/issues/40623
{
assert.strictEqual(performance.timerify(function func() {
return 1;
})(), 1);
assert.strictEqual(performance.timerify(function() {
return 1;
})(), 1);
assert.strictEqual(performance.timerify(() => {
return 1;
})(), 1);
class C {}
const wrap = performance.timerify(C);
assert.ok(new wrap() instanceof C);
assert.throws(() => wrap(), {
name: 'TypeError',
});
}

0 comments on commit 06221a7

Please sign in to comment.