Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 40 additions & 53 deletions src/js/node/perf_hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,68 +276,55 @@ function processTimerifyComplete(name, start, args, histogram) {
}
}

// Node-only members go on Performance.prototype (non-enumerable, like node).
// Not getPrototypeOf(performance): a replaced global would land these on
// Object.prototype. hasOwn so a future native own-prop suppresses the stub.
const PerformancePrototype = Performance?.prototype;
if (PerformancePrototype) {
if (!Object.hasOwn(PerformancePrototype, "nodeTiming")) {
Object.defineProperty(PerformancePrototype, "nodeTiming", {
__proto__: null,
value: createPerformanceNodeTiming(),
writable: true,
enumerable: false,
configurable: true,
});
}
if (!Object.hasOwn(PerformancePrototype, "eventLoopUtilization")) {
Object.defineProperty(PerformancePrototype, "eventLoopUtilization", {
__proto__: null,
value: eventLoopUtilization,
writable: true,
enumerable: false,
configurable: true,
});
}
if (!Object.hasOwn(PerformancePrototype, "timerify")) {
Object.defineProperty(PerformancePrototype, "timerify", {
__proto__: null,
value: timerify,
writable: true,
enumerable: false,
configurable: true,
});
}
}

export default {
timerify,
performance: {
mark(_) {
return performance.mark(...arguments);
},
measure(_) {
return performance.measure(...arguments);
},
clearMarks(_) {
return performance.clearMarks(...arguments);
},
clearMeasures(_) {
return performance.clearMeasures(...arguments);
},
getEntries(_) {
return performance.getEntries(...arguments);
},
getEntriesByName(_) {
return performance.getEntriesByName(...arguments);
},
getEntriesByType(_) {
return performance.getEntriesByType(...arguments);
},
setResourceTimingBufferSize(_) {
return performance.setResourceTimingBufferSize(...arguments);
},
timeOrigin: performance.timeOrigin,
toJSON(_) {
return performance.toJSON(...arguments);
},
onresourcetimingbufferfull: performance.onresourcetimingbufferfull,
nodeTiming: createPerformanceNodeTiming(),
now: () => performance.now(),
timerify,
eventLoopUtilization: eventLoopUtilization,
clearResourceTimings: function () {},
},
// performance: {
// clearMarks: [Function: clearMarks],
// clearMeasures: [Function: clearMeasures],
// clearResourceTimings: [Function: clearResourceTimings],
// getEntries: [Function: getEntries],
// getEntriesByName: [Function: getEntriesByName],
// getEntriesByType: [Function: getEntriesByType],
// mark: [Function: mark],
// measure: [Function: measure],
// now: performance.now,
// setResourceTimingBufferSize: [Function: setResourceTimingBufferSize],
// timeOrigin: performance.timeOrigin,
// toJSON: [Function: toJSON],
// onresourcetimingbufferfull: [Getter/Setter]
// },
performance,
constants,
// Read off the same prototype the defines target, so a replaced global
// `performance` cannot make these undefined, and a future native own-prop
// is what the export picks up.
eventLoopUtilization: PerformancePrototype?.eventLoopUtilization ?? eventLoopUtilization,
timerify: PerformancePrototype?.timerify ?? timerify,
Performance,
PerformanceEntry,
PerformanceMark,
PerformanceMeasure,
PerformanceObserver: PerformanceObserverForNodeTypes,
PerformanceObserverEntryList,
PerformanceNodeTiming,
eventLoopUtilization,
monitorEventLoopDelay: function monitorEventLoopDelay(options?: { resolution?: number }) {
const impl = require("internal/perf_hooks/monitorEventLoopDelay");
return impl(options);
Expand Down
105 changes: 99 additions & 6 deletions test/js/node/perf_hooks/perf_hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import { bunEnv, bunExe } from "harness";
import net from "net";
import perf, { PerformanceObserver } from "perf_hooks";

test("stubs", () => {
expect(perf.performance.nodeTiming).toBeObject();

expect(perf.performance.now()).toBeNumber();
expect(perf.performance.timeOrigin).toBeNumber();
expect(perf.performance.eventLoopUtilization()).toBeObject();
// Like node, require("node:perf_hooks").performance is the global performance
// object itself, not a separate wrapper.
test("perf_hooks.performance is the global performance object", () => {
expect(perf.performance).toBe(globalThis.performance);
});

test("doesn't throw", () => {
Expand Down Expand Up @@ -58,6 +56,8 @@ test("timerify entry shape", async () => {
test("timerify is exposed on both performance and as a top-level export (Node v25.2+)", () => {
expect(perf.performance.timerify).toBeFunction();
expect(perf.timerify).toBeFunction();
// Same function object, matching node's lib/perf_hooks.js module.exports.
expect(perf.timerify).toBe(perf.performance.timerify);
});

// Captured from the real node v26.3.0 binary:
Expand Down Expand Up @@ -159,3 +159,96 @@ test("net entries are instanceof PerformanceEntry", async () => {
expect(entry.constructor.name).toBe("PerformanceNodeEntry");
expect(entry.entryType).toBe("net");
});

test("node-only members are present on performance", () => {
expect(perf.performance.nodeTiming).toBeObject();
expect(perf.performance.now()).toBeNumber();
expect(perf.performance.timeOrigin).toBeNumber();
expect(perf.performance.eventLoopUtilization).toBeFunction();
expect(perf.performance.eventLoopUtilization()).toEqual({
idle: expect.any(Number),
active: expect.any(Number),
utilization: expect.any(Number),
});
});

// markResourceTiming / clearResourceTimings were missing / a JS no-op before the
// module exported the global; now they are the global's own methods.
test("resource-timing methods are present and callable", () => {
expect(perf.performance.markResourceTiming).toBeFunction();
expect(() => perf.performance.markResourceTiming()).not.toThrow();
expect(perf.performance.clearResourceTimings).toBeFunction();
expect(() => perf.performance.clearResourceTimings()).not.toThrow();
});

// node puts the node-only members on Performance.prototype, non-enumerable, so
// Object.keys(performance) is unchanged.
test("node-only members live on the prototype, non-enumerable", () => {
const proto = Object.getPrototypeOf(globalThis.performance);
for (const key of ["nodeTiming", "eventLoopUtilization", "timerify"]) {
const descriptor = Object.getOwnPropertyDescriptor(proto, key);
expect(descriptor).toBeDefined();
expect(descriptor!.enumerable).toBe(false);
}
expect(Object.keys(globalThis.performance)).not.toContain("nodeTiming");
expect(Object.keys(globalThis.performance)).not.toContain("eventLoopUtilization");
expect(Object.keys(globalThis.performance)).not.toContain("timerify");
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

// The members are installed on the prototype by node:perf_hooks; the bare web
// global has none of them until the module is loaded. A future native move (onto
// JSPerformance.cpp) would make the first half eager and require this to change.
test("node-only members are installed onto the prototype when node:perf_hooks is loaded", async () => {
await using proc = Bun.spawn({
cmd: [
bunExe(),
"-e",
`const proto = Performance.prototype;
const keys = ["nodeTiming", "eventLoopUtilization", "timerify"];
const describe = () => keys.map(k => {
const d = Object.getOwnPropertyDescriptor(proto, k);
return k + "=" + typeof globalThis.performance[k] + (d ? "|proto|enum=" + d.enumerable : "|absent");
}).join(" ");
console.log("before: " + describe());
require("node:perf_hooks");
console.log("after: " + describe());`,
],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
if (exitCode !== 0) expect(stderr).toBe("");
expect({ stdout, exitCode }).toEqual({
stdout:
"before: nodeTiming=undefined|absent eventLoopUtilization=undefined|absent timerify=undefined|absent\n" +
"after: nodeTiming=object|proto|enum=false eventLoopUtilization=function|proto|enum=false timerify=function|proto|enum=false\n",
exitCode: 0,
});
});

// onresourcetimingbufferfull is the global's own accessor; since the module
// object is the global, assigning through either reaches the same object.
test("onresourcetimingbufferfull is the global's accessor", () => {
const previous = globalThis.performance.onresourcetimingbufferfull;
try {
const listener = () => {};
perf.performance.onresourcetimingbufferfull = listener;
expect(globalThis.performance.onresourcetimingbufferfull).toBe(listener);
} finally {
globalThis.performance.onresourcetimingbufferfull = previous;
}
});

// node's lib/perf_hooks.js lists eventLoopUtilization in module.exports as well
// as on `performance`, and the two are the same function object. (It is absent
// from the module exports on v22 and earlier; this matches current node.)
test("perf_hooks exports eventLoopUtilization at the module level", () => {
expect(perf.eventLoopUtilization).toBeFunction();
expect(perf.eventLoopUtilization).toBe(perf.performance.eventLoopUtilization);
expect(perf.eventLoopUtilization()).toEqual({
idle: expect.any(Number),
active: expect.any(Number),
utilization: expect.any(Number),
});
});
Loading