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

Commit

Permalink
Extend fast cases to multiple listeners.
Browse files Browse the repository at this point in the history
Previously, only the case with 1 listener
was optimized for less than 3 event arguments.
This is now extended to any number of listeners.
  • Loading branch information
RubenVerborgh committed Jan 11, 2015
1 parent e4d4c98 commit ca3a08d
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,30 @@ EventEmitter.prototype.emit = function emit(type, arg1, arg2) {
args[i - 1] = arguments[i];
handler.apply(this, args);
}
} else if (util.isObject(handler)) {
args = new Array(len - 1);
for (i = 1; i < len; i++)
args[i - 1] = arguments[i];

} else if (util.isArray(handler)) {
listeners = handler.slice();
len = listeners.length;
for (i = 0; i < len; i++)
listeners[i].apply(this, args);
switch (len) {
// fast cases
case 1:
for (i = 0, len = listeners.length; i < len; i++)
listeners[i].call(this);
break;
case 2:
for (i = 0, len = listeners.length; i < len; i++)
listeners[i].call(this, arg1);
break;
case 3:
for (i = 0, len = listeners.length; i < len; i++)
listeners[i].call(this, arg1, arg2);
break;
// slower
default:
args = new Array(len - 1);
for (i = 1; i < len; i++)
args[i - 1] = arguments[i];
for (i = 0, len = listeners.length; i < len; i++)
listeners[i].apply(this, args);
}
}

if (this.domain && this !== process)
Expand Down

0 comments on commit ca3a08d

Please sign in to comment.