From c7b2e29e5e4b2a20e0cbc34d56c57d96720aadc2 Mon Sep 17 00:00:00 2001 From: isaacs Date: Thu, 25 Oct 2012 07:48:36 -0700 Subject: [PATCH] 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. --- lib/events.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/events.js b/lib/events.js index e0af38066a6..4ef2daed925 100644 --- a/lib/events.js +++ b/lib/events.js @@ -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;