EventEmitter.removeListener removes only one listener #5211

CrabDude opened this Issue Apr 3, 2013 · 2 comments

3 participants


This is different from a lot of other libraries (jQuery) and removeEventListener in the DOM where the same listener can be added multiple times, and removal removes all handlers for for the same event/listener combo.

It seems like the only way to determine if all events of the same type/handler are removed, is to listen for removeListener and keep calling removeListener until it's no longer emitted, or to manually iterate through the listener array checking for listener.

Example of issue:

function foo() {}

process.on('foo', foo)
process.on('foo', foo)
process.emit('foo') // foo called twice

process.removeListener('foo', foo)
process.emit('foo') // foo still called

Is this by design? It would cause memory leaks if someone was not familiar with this issue and was extending and creating a a long lasting EventEmitter.

Node.js Foundation member

Is this by design?

Maybe not originally but it's pretty much codified now. I don't want to change the behavior either; EventEmitter#removeListener() gets called a lot and is already quite expensive. Doing mandatory full scans over the listeners array would make that a lot worse.

@bnoordhuis bnoordhuis closed this Apr 3, 2013

The event subsystem is locked at this point. I'd be willing to accept refactoring changes to improve performance, but any interface changes should be considered final as of 0.10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment