diff --git a/lib/internal/perf/timerify.js b/lib/internal/perf/timerify.js index 38f6b3d37acdd2..dc16b4c6c405ef 100644 --- a/lib/internal/perf/timerify.js +++ b/lib/internal/perf/timerify.js @@ -20,10 +20,6 @@ const { isHistogram } = require('internal/histogram'); -const { - isConstructor, -} = internalBinding('util'); - const { codes: { ERR_INVALID_ARG_TYPE, @@ -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, diff --git a/test/parallel/test-performance-function.js b/test/parallel/test-performance-function.js index fcc3004d02884a..5f774d6c2adcf5 100644 --- a/test/parallel/test-performance-function.js +++ b/test/parallel/test-performance-function.js @@ -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', + }); +}