Permalink
Browse files

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.
  • Loading branch information...
1 parent 4b8629d commit c7b2e29e5e4b2a20e0cbc34d56c57d96720aadc2 @isaacs isaacs committed Oct 25, 2012
Showing with 4 additions and 5 deletions.
  1. +4 −5 lib/events.js
View
9 lib/events.js
@@ -23,13 +23,16 @@ 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');
if (domain.active && !(this instanceof domain.Domain)) {
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;

0 comments on commit c7b2e29

Please sign in to comment.