Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: remove code duplication #12655

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 42 additions & 61 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,55 +100,53 @@ EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
// arguments and can be deoptimized because of that. These functions always have
// the same number of arguments and thus do not get deoptimized, so the code
// inside them can execute faster.
function emitNone(handler, isFn, self) {
if (isFn)
handler.call(self);
else {
var len = handler.length;
var listeners = arrayClone(handler, len);
for (var i = 0; i < len; ++i)
listeners[i].call(self);
}
function emitNone(handler, self) {
handler.call(self);
}
function emitOne(handler, isFn, self, arg1) {
if (isFn)
handler.call(self, arg1);
else {
var len = handler.length;
var listeners = arrayClone(handler, len);
for (var i = 0; i < len; ++i)
listeners[i].call(self, arg1);
}
function emitOne(handler, self, args) {
handler.call(self, args[0]);
}
function emitTwo(handler, isFn, self, arg1, arg2) {
if (isFn)
handler.call(self, arg1, arg2);
else {
var len = handler.length;
var listeners = arrayClone(handler, len);
for (var i = 0; i < len; ++i)
listeners[i].call(self, arg1, arg2);
}
function emitTwo(handler, self, args) {
handler.call(self, args[0], args[1]);
}
function emitThree(handler, isFn, self, arg1, arg2, arg3) {
if (isFn)
handler.call(self, arg1, arg2, arg3);
else {
var len = handler.length;
var listeners = arrayClone(handler, len);
for (var i = 0; i < len; ++i)
listeners[i].call(self, arg1, arg2, arg3);
}
function emitThree(handler, self, args) {
handler.call(self, args[0], args[1], args[2]);
}

function emitMany(handler, self, args) {
handler.apply(self, args);
}

function emitMany(handler, isFn, self, args) {
function _emit(handler, isFn, self, args) {
var runHandler;
var len = args.length;

switch (len) {
// fast cases
case 0:
runHandler = emitNone;
break;
case 1:
runHandler = emitOne;
break;
case 2:
runHandler = emitTwo;
break;
case 3:
runHandler = emitThree;
break;
// slower
default:
runHandler = emitMany;
}

if (isFn)
handler.apply(self, args);
runHandler(handler, self, args);
else {
var len = handler.length;
len = handler.length;
var listeners = arrayClone(handler, len);
for (var i = 0; i < len; ++i)
listeners[i].apply(self, args);
runHandler(listeners[i], self, args);
}
}

Expand Down Expand Up @@ -201,27 +199,10 @@ EventEmitter.prototype.emit = function emit(type) {

var isFn = typeof handler === 'function';
len = arguments.length;
switch (len) {
// fast cases
case 1:
emitNone(handler, isFn, this);
break;
case 2:
emitOne(handler, isFn, this, arguments[1]);
break;
case 3:
emitTwo(handler, isFn, this, arguments[1], arguments[2]);
break;
case 4:
emitThree(handler, isFn, this, arguments[1], arguments[2], arguments[3]);
break;
// slower
default:
args = new Array(len - 1);
for (i = 1; i < len; i++)
args[i - 1] = arguments[i];
emitMany(handler, isFn, this, args);
}
args = new Array(len - 1);
for (i = 1; i < len; i++)
args[i - 1] = arguments[i];
_emit(handler, isFn, this, args);

if (needDomainExit)
domain.exit();
Expand Down
8 changes: 4 additions & 4 deletions test/message/stdin_messages.out
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ SyntaxError: Strict mode code may not include a with statement
at evalScript (bootstrap_node.js:*:*)
at Socket.<anonymous> (bootstrap_node.js:*:*)
at emitNone (events.js:*:*)
at _emit (events.js:*:*)
at Socket.emit (events.js:*:*)
at endReadableNT (_stream_readable.js:*:*)
at _combinedTickCallback (internal/process/next_tick.js:*:*)
42
42
[stdin]:1
Expand All @@ -28,8 +28,8 @@ Error: hello
at evalScript (bootstrap_node.js:*:*)
at Socket.<anonymous> (bootstrap_node.js:*:*)
at emitNone (events.js:*:*)
at _emit (events.js:*:*)
at Socket.emit (events.js:*:*)
at endReadableNT (_stream_readable.js:*:*)
[stdin]:1
throw new Error("hello")
^
Expand All @@ -43,8 +43,8 @@ Error: hello
at evalScript (bootstrap_node.js:*:*)
at Socket.<anonymous> (bootstrap_node.js:*:*)
at emitNone (events.js:*:*)
at _emit (events.js:*:*)
at Socket.emit (events.js:*:*)
at endReadableNT (_stream_readable.js:*:*)
100
[stdin]:1
var x = 100; y = x;
Expand All @@ -59,8 +59,8 @@ ReferenceError: y is not defined
at evalScript (bootstrap_node.js:*:*)
at Socket.<anonymous> (bootstrap_node.js:*:*)
at emitNone (events.js:*:*)
at _emit (events.js:*:*)
at Socket.emit (events.js:*:*)
at endReadableNT (_stream_readable.js:*:*)

[stdin]:1
var ______________________________________________; throw 10
Expand Down