Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix number of args emitted by EventEmitter during "fast case" (lte 3 …

…args)
  • Loading branch information...
commit 3bb61a913b95e03c420f230e355521e603078fd9 1 parent 06b600b
@jmar777 jmar777 authored ry committed
Showing with 41 additions and 7 deletions.
  1. +14 −7 lib/events.js
  2. +27 −0 test/simple/test-event-emitter-num-args.js
View
21 lib/events.js
@@ -22,20 +22,27 @@ EventEmitter.prototype.emit = function (type) {
if (!handler) return false;
if (typeof handler == 'function') {
- if (arguments.length <= 3) {
- // fast case
- handler.call(this, arguments[1], arguments[2]);
- } else {
+ switch (arguments.length) {
+ // fast cases
+ case 1:
+ handler.call(this);
+ break;
+ case 2:
+ handler.call(this, arguments[1]);
+ break;
+ case 3:
+ handler.call(this, arguments[1], arguments[2]);
+ break;
// slower
- var args = Array.prototype.slice.call(arguments, 1);
- handler.apply(this, args);
+ default:
+ var args = Array.prototype.slice.call(arguments, 1);
+ handler.apply(this, args);
}
return true;
} else if (isArray(handler)) {
var args = Array.prototype.slice.call(arguments, 1);
-
var listeners = handler.slice();
for (var i = 0, l = listeners.length; i < l; i++) {
listeners[i].apply(this, args);
View
27 test/simple/test-event-emitter-num-args.js
@@ -0,0 +1,27 @@
+common = require("../common");
+assert = common.assert
+var events = require('events');
+
+var e = new events.EventEmitter(),
+ num_args_emited = [];
+
+e.on("numArgs", function() {
+ var numArgs = arguments.length;
+ console.log("numArgs: " + numArgs);
+ num_args_emited.push(numArgs);
+});
+
+console.log("start");
+
+e.emit("numArgs");
+e.emit("numArgs", null);
+e.emit("numArgs", null, null);
+e.emit("numArgs", null, null, null);
+e.emit("numArgs", null, null, null, null);
+e.emit("numArgs", null, null, null, null, null);
+
+process.addListener("exit", function () {
+ assert.deepEqual([0, 1, 2, 3, 4, 5], num_args_emited);
+});
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.