events: Make the EventEmitter class monomorphic

Always add domain, _events, and _maxListeners properties, set to the
default values at first.

Leads to a very very slight perf improvement when using setMaxListeners,
or dealing with a lot of EE objects that don't have any listeners.
v0.8.16-release
isaacs 2012-10-25 07:48:36 -07:00
parent 4b8629db37
commit c7b2e29e5e
1 changed files with 4 additions and 5 deletions

View File

@ -23,6 +23,7 @@ var isArray = Array.isArray;
var domain;
function EventEmitter() {
this.domain = null;
if (exports.usingDomains) {
// if there is an active domain, then attach to it.
domain = domain || require('domain');
@ -30,6 +31,8 @@ function EventEmitter() {
this.domain = domain.active;
}
}
this._events = null;
this._maxListeners = defaultMaxListeners;
}
exports.EventEmitter = EventEmitter;
@ -161,11 +164,7 @@ EventEmitter.prototype.addListener = function(type, listener) {
// Check for listener leak
if (isArray(this._events[type]) && !this._events[type].warned) {
var m;
if (this._maxListeners !== undefined) {
m = this._maxListeners;
} else {
m = defaultMaxListeners;
}
m = this._maxListeners;
if (m && m > 0 && this._events[type].length > m) {
this._events[type].warned = true;