Skip to content
Merged
Changes from 3 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
52 changes: 39 additions & 13 deletions ext/node/polyfills/perf_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,56 @@ class PerformanceObserver extends WebPerformanceObserver {
}
}

performance.eventLoopUtilization = () => {
const eventLoopUtilization = () => {
// TODO(@marvinhagemeister): Return actual non-stubbed values
return { idle: 0, active: 0, utilization: 0 };
};

performance.eventLoopUtilization = eventLoopUtilization;

performance.nodeTiming = {};

Comment thread
bartlomieju marked this conversation as resolved.
performance.timerify = (fn) => {
const timerify = (fn, options = {}) => {
if (typeof fn !== "function") {
throw new TypeError("The 'fn' argument must be of type function");
throw new ERR_INVALID_ARG_TYPE("fn", "function", fn);
}
const wrapped = (...args) => {
const start = performance.now();
const result = fn(...args);
const end = performance.now();

performance.measure(`timerify(${fn.name || "anonymous"})`, { start, end });
if (options !== undefined && (typeof options !== "object" || options === null)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI lint fails here — dprint wants this if condition wrapped:

if (
  options !== undefined && (typeof options !== "object" || options === null)
) {
  throw new ERR_INVALID_ARG_TYPE("options", "Object", options);
}

./tools/format.js will auto-fix. Full output at https://github.com/denoland/deno/actions/runs/25001671896/job/73213243736 (3 platforms all show the same one-file diff).

throw new ERR_INVALID_ARG_TYPE("options", "Object", options);
}

return result;
};
if (options?.histogram !== undefined) {
if (
typeof options.histogram !== "object" ||
options.histogram === null ||
typeof options.histogram.record !== "function"
) {
throw new ERR_INVALID_ARG_TYPE(
"options.histogram",
"RecordableHistogram",
options.histogram,
);
}
Comment thread
bartlomieju marked this conversation as resolved.
}

function timerified(...args) {
// TODO(bartlomieju): emit PerformanceEntry with entryType 'function'
Comment thread
bartlomieju marked this conversation as resolved.
return new.target ? new fn(...args) : fn.apply(this, args);
}

Object.defineProperty(wrapped, "name", {
value: fn.name || "wrapped",
Object.defineProperty(timerified, "name", {
value: `timerified ${fn.name}`,
configurable: true,
});
Object.defineProperty(timerified, "length", {
value: fn.length,
configurable: true,
});

return wrapped;
return timerified;
};

performance.timerify = timerify;
// TODO(bartlomieju):
performance.markResourceTiming = () => {};

Expand All @@ -97,14 +119,18 @@ export default {
PerformanceObserverEntryList,
PerformanceEntry,
monitorEventLoopDelay,
eventLoopUtilization,
timerify,
constants,
};

export {
constants,
eventLoopUtilization,
monitorEventLoopDelay,
performance,
PerformanceEntry,
PerformanceObserver,
PerformanceObserverEntryList,
timerify,
};
Loading