diff --git a/lib/internal/perf/timerify.js b/lib/internal/perf/timerify.js index 6bfede7aa1fa20..f7c66af9193470 100644 --- a/lib/internal/perf/timerify.js +++ b/lib/internal/perf/timerify.js @@ -1,7 +1,6 @@ 'use strict'; const { - FunctionPrototypeBind, ObjectDefineProperties, MathCeil, ReflectApply, @@ -75,18 +74,17 @@ function timerify(fn, options = {}) { const result = constructor ? ReflectConstruct(fn, args, fn) : ReflectApply(fn, this, args); - if (!constructor && typeof result?.finally === 'function') { - return result.finally( - FunctionPrototypeBind( - processComplete, - result, - fn.name, - start, - args, - histogram)); + if (!constructor && typeof result?.then === 'function') { + return result.then( + function wrappedTimerifiedPromise(value) { + processComplete(fn.name, start, args, histogram); + return value; + } + ); } processComplete(fn.name, start, args, histogram); return result; + } ObjectDefineProperties(timerified, { diff --git a/test/parallel/test-performance-function.js b/test/parallel/test-performance-function.js index fcc3004d02884a..2160f828c97ddd 100644 --- a/test/parallel/test-performance-function.js +++ b/test/parallel/test-performance-function.js @@ -89,6 +89,20 @@ const { assert.strictEqual(p.name, 'timerified timerified m'); } +// Function +{ + const f1 = () => 1; + const f2 = async () => 2; + const h1 = createHistogram(); + const h2 = createHistogram(); + const g1 = performance.timerify(f1, { histogram: h1 }); + const g2 = performance.timerify(f2, { histogram: h2 }); + g1(); + g2().then(common.mustCall(() => { + assert.strictEqual(h1.count, h2.count); + })); +} + (async () => { const histogram = createHistogram(); const m = (a, b = 1) => {};