There are many top-level functions in node:events
that mirror their equivalents on the EventEmitter prototype, but also accept EventTarget arguments. (getEventListeners()
, getMaxListeners()
, etc.)
events.listenerCount()
was previously deprecated on the basis of duplicating the functionality of EventEmitter.prototype.listenerCount()
. However, there's no equivalent function on the EventTarget prototype, and it seems like this would be a good candidate to follow the others and become an agnostic utility function.
const { listenerCount } = require('events')
const et = new EventTarget()
et.addEventListener('event', () => {})
listenerCount(et, 'event') // 1
An alternative for this functionality exists via getEventListeners(...).length
, but since listenerCount()
already exists (and has an analogue on the EventEmitter prototype that isn't going anywhere), it seems like this would be a low-effort improvement.