EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them.
interface
perf_hooks.Performance
interface Performance
- type: K,): void;
Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
type: string,): void;Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
- ): boolean;
Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
The
eventLoopUtilization()method returns an object that contains the cumulative duration of time the event loop has been both idle and active as a high resolution milliseconds timer. Theutilizationvalue is the calculated Event Loop Utilization (ELU).If bootstrapping has not yet finished on the main thread the properties have the value of
0. The ELU is immediately available on Worker threads since bootstrap happens within the event loop.Both
utilization1andutilization2are optional parameters.If
utilization1is passed, then the delta between the current call'sactiveandidletimes, as well as the correspondingutilizationvalue are calculated and returned (similar toprocess.hrtime()).If
utilization1andutilization2are both passed, then the delta is calculated between the two arguments. This is a convenience option because, unlikeprocess.hrtime(), calculating the ELU is more complex than a single subtraction.ELU is similar to CPU utilization, except that it only measures event loop statistics and not CPU usage. It represents the percentage of time the event loop has spent outside the event loop's event provider (e.g.
epoll_wait). No other CPU idle time is taken into consideration. The following is an example of how a mostly idle process will have a high ELU.import { eventLoopUtilization } from 'node:perf_hooks'; import { spawnSync } from 'node:child_process'; setImmediate(() => { const elu = eventLoopUtilization(); spawnSync('sleep', ['5']); console.log(eventLoopUtilization(elu).utilization); });Although the CPU is mostly idle while running this script, the value of
utilizationis1. This is because the call tochild_process.spawnSync()blocks the event loop from proceeding.Passing in a user-defined object instead of the result of a previous call to
eventLoopUtilization()will lead to undefined behavior. The return values are not guaranteed to reflect any correct state of the event loop.@param utilization1The result of a previous call to
eventLoopUtilization().@param utilization2The result of a previous call to
eventLoopUtilization()prior toutilization1.- requestedUrl: string,initiatorType: string,global: unknown,cacheMode: string,bodyInfo: unknown,responseStatus: number,deliveryType?: string
- measureName: string,endMark?: string
- type: K,options?: boolean | EventListenerOptions): void;
Removes the event listener in target's event listener list with the same type, callback, and options.
type: string,options?: boolean | EventListenerOptions): void;Removes the event listener in target's event listener list with the same type, callback, and options.
- fn: T,): T;
This property is an extension by Node.js. It is not available in Web browsers.
Wraps a function within a new function that measures the running time of the wrapped function. A
PerformanceObservermust be subscribed to the'function'event type in order for the timing details to be accessed.import { performance, PerformanceObserver } from 'node:perf_hooks'; function someFunction() { console.log('hello world'); } const wrapped = performance.timerify(someFunction); const obs = new PerformanceObserver((list) => { console.log(list.getEntries()[0].duration); performance.clearMarks(); performance.clearMeasures(); obs.disconnect(); }); obs.observe({ entryTypes: ['function'] }); // A performance timeline entry will be created wrapped();If the wrapped function returns a promise, a finally handler will be attached to the promise and the duration will be reported once the finally handler is invoked.