From: Ryan Dahl Date: Fri, 23 Apr 2010 00:31:35 +0000 (-0700) Subject: Optimize for few args in EventEmitter.emit X-Git-Tag: v0.1.92~8 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0633e5cac981cf6433baeea581b34461e3f82dc2;p=platform%2Fupstream%2Fnodejs.git Optimize for few args in EventEmitter.emit --- diff --git a/lib/events.js b/lib/events.js index 0ac8a69..1210022 100644 --- a/lib/events.js +++ b/lib/events.js @@ -2,9 +2,10 @@ exports.EventEmitter = process.EventEmitter; process.EventEmitter.prototype.emit = function (type) { - if (type == 'error') { + // If there is no 'error' event listener then throw. + if (type === 'error') { if (!this._events || !this._events.error || - (this._events.error instanceof Array && !this._events.error.length)) + (this._events.error instanceof Array && !this._events.error.length)) { if (arguments[1] instanceof Error) { throw arguments[1]; @@ -19,15 +20,23 @@ process.EventEmitter.prototype.emit = function (type) { if (!this._events[type]) return false; if (typeof this._events[type] == 'function') { - var args = Array.prototype.slice.call(arguments, 1); - - this._events[type].apply(this, args); + if (arguments.length < 3) { + // fast case + this._events[type].call( this + , arguments[1] + , arguments[2] + ); + } else { + // slower + var args = Array.prototype.slice.call(arguments, 1); + this._events[type].apply(this, args); + } return true; } else if (this._events[type] instanceof Array) { var args = Array.prototype.slice.call(arguments, 1); - + var listeners = this._events[type].slice(0); for (var i = 0, l = listeners.length; i < l; i++) { listeners[i].apply(this, args);