Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

events: use Infinity for setMaxListeners instead of 0 #22987

Closed
jasonkarns opened this issue Apr 27, 2015 · 2 comments
Closed

events: use Infinity for setMaxListeners instead of 0 #22987

jasonkarns opened this issue Apr 27, 2015 · 2 comments

Comments

@jasonkarns
Copy link
Member

Is there a sound reason for choosing 0 as the magic value that disables the max listeners? It's essentially using 0 as a special value to represent unlimited but JS already has a perfectly valid number to represent infinity: Infinity. It would simplify the memory-leak checking as the 0 special case would no longer need checked. Any number would naturally return true for n < Infinity.

@misterdjules
Copy link

In node v0.10.x and node v0.12.x, both 0 and Infinity are supported:

➜  v0.12 git:(v0.12) cat ~/dev/repros/gh-22987.js   
var EventEmitter = require('events').EventEmitter;

var eventEmitterInfinity = new EventEmitter();

eventEmitterInfinity.setMaxListeners(Infinity);

for (var i = 0; i < EventEmitter.defaultMaxListeners + 1; ++i) {
  eventEmitterInfinity.on('foo', function bar() {});
}

var eventEmitterZero = new EventEmitter();

eventEmitterZero.setMaxListeners(0);

for (var i = 0; i < EventEmitter.defaultMaxListeners + 1; ++i) {
  eventEmitterZero.on('foo', function bar() {});
}
➜  v0.12 git:(v0.12) ./node --version
v0.12.3-pre
➜  v0.12 git:(v0.12) ./node ~/dev/repros/gh-22987.js
➜  v0.12 git:(v0.12) node --version
v0.10.38
➜  v0.12 git:(v0.12) node ~/dev/repros/gh-22987.js  
➜  v0.12 git:(v0.12)

I'm not sure that slightly simplifying this code is worth not accepting 0 as unlimited and breaking the API. So closing for now, but please feel free to comment further if you still think it would be worth it.

@jasonkarns
Copy link
Member Author

In lieu of changing the API, perhaps the documentation could at least be updated to recommend using Infinity instead of magic-value 0. This would pave the way for (potentially) deprecating and eventually removing 0 as the magic value.

jasonkarns added a commit to jasonkarns/node that referenced this issue Aug 26, 2015
Instead of recommending `0` as the magic value to set max listeners to unlimited, recommend `Infinity`.

This paves the way for `0` as a magic value eventually being deprecated and finally removed

closes nodejs/node-v0.x-archive#22987
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants