Skip to content

Commit 30cf1f4

Browse files
authored
fix(ext/node): implement performance.timerify() (#31238)
Adds the missing implementation for `performance.timerify()` in `node:perf_hooks`, which previously resulted in a "Not implemented" error. Fixes #31115
1 parent 56dd527 commit 30cf1f4

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

ext/node/polyfills/perf_hooks.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// TODO(petamoriken): enable prefer-primordials for node polyfills
44
// deno-lint-ignore-file prefer-primordials
55

6-
import { notImplemented } from "ext:deno_node/_utils.ts";
76
import { performance, PerformanceEntry } from "ext:deno_web/15_performance.js";
87
import { EldHistogram } from "ext:core/ops";
98

@@ -24,12 +23,29 @@ performance.eventLoopUtilization = () => {
2423
return { idle: 0, active: 0, utilization: 0 };
2524
};
2625

27-
// TODO(bartlomieju):
2826
performance.nodeTiming = {};
2927

30-
// TODO(bartlomieju):
31-
performance.timerify = () => notImplemented("timerify from performance");
28+
performance.timerify = (fn) => {
29+
if (typeof fn !== "function") {
30+
throw new TypeError("The 'fn' argument must be of type function");
31+
}
32+
const wrapped = (...args) => {
33+
const start = performance.now();
34+
const result = fn(...args);
35+
const end = performance.now();
36+
37+
performance.measure(`timerify(${fn.name || "anonymous"})`, { start, end });
3238

39+
return result;
40+
};
41+
42+
Object.defineProperty(wrapped, "name", {
43+
value: fn.name || "wrapped",
44+
configurable: true,
45+
});
46+
47+
return wrapped;
48+
};
3349
// TODO(bartlomieju):
3450
performance.markResourceTiming = () => {};
3551

tests/unit_node/timers_test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ import { assert, assertRejects, fail } from "@std/assert";
44
import * as timers from "node:timers";
55
import * as timersPromises from "node:timers/promises";
66
import { assertEquals } from "@std/assert";
7+
import { performance } from "node:perf_hooks";
8+
9+
Deno.test("[node/perf_hooks] performance.timerify()", () => {
10+
function sayHello() {
11+
return "hello world";
12+
}
13+
14+
const wrapped = performance.timerify(sayHello);
15+
const result = wrapped();
16+
17+
if (result !== "hello world") {
18+
throw new Error(`Expected "hello world", got "${result}"`);
19+
}
20+
});
721

822
Deno.test("[node/timers setTimeout]", () => {
923
{

0 commit comments

Comments
 (0)