Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Difference Between EventEmitter and NodeEventTarget

  • Last Updated : 01 Sep, 2020

EventEmitter: All EventEmitters emit the event ‘newListener‘ when new listeners are added and ‘removeListener‘ when existing listeners are removed. It is defined and exposed by the events module:

To import EventEmitter, use the following import statement:

const EventEmitter = require('events');

NodeEventTarget: The EventTarget and Event objects are a Node.js-specific implementation of the EventTarget Web API that is exposed by some Node.js core APIs.

Difference between EventEmitter and NodeEventTarget:

Event EmitterNode Event Target
It is inherited from the events Module of JavaScript.It is a modified subset of EventEmitter API and inherited from it.
It implements is-a relationship with the events Module.It implements is-a relationship with the EventTarget API.
In eventEmitter, for the same event, we can allow multiple listeners to be registered.Any listener can be registered once per event type and it gets ignored if attempted to register a listener multiple times.
It emulates the most from Events such as ‘error’, ‘Classes’, Emits, etc. It does not emulate the full EventEmitter APIs like prependListener(), prependOnceListener(), rawListeners(), etc.
Its default behavior is to log information and end the current execution.For ‘error‘ type events, it does not implement any default behavior.
 If an error occurs within an EventEmitter instance, then the typical action is for an ‘error’ event to be emitted. It supports EventListener objects and functions as handlers for all event types.
 All EventEmitter emit the event ‘newListener’, when new listeners are added and ‘removeListener’ when listeners are removed.It isn’t an instance of EventEmitter and in most cases, it can’t be used in place of an EventEmitter.

Syntax:

emitter.once(
  eventName, listener)

Syntax:

nodeEventTarget.once(
  type, listener[, options])

Reference: https://nodejs.org/api/events.html#events_nodeeventtarget_vs_eventemitter

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!