-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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 emit micro-optimizations #16869
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
const EventEmitter = require('events').EventEmitter; | ||
|
||
const bench = common.createBenchmark(main, { n: [2e6] }); | ||
|
||
function main(conf) { | ||
const n = conf.n | 0; | ||
|
||
const ee = new EventEmitter(); | ||
|
||
for (var k = 0; k < 10; k += 1) | ||
ee.on('dummy', function() {}); | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
ee.emit('dummy', 5, true, 'something', false); | ||
} | ||
bench.end(n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,63 +105,6 @@ EventEmitter.prototype.getMaxListeners = function getMaxListeners() { | |
return $getMaxListeners(this); | ||
}; | ||
|
||
// These standalone emit* functions are used to optimize calling of event | ||
// handlers for fast cases because emit() itself often has a variable number of | ||
// 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 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 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 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 emitMany(handler, isFn, self, args) { | ||
if (isFn) | ||
handler.apply(self, args); | ||
else { | ||
var len = handler.length; | ||
var listeners = arrayClone(handler, len); | ||
for (var i = 0; i < len; ++i) | ||
listeners[i].apply(self, args); | ||
} | ||
} | ||
|
||
EventEmitter.prototype.emit = function emit(type, ...args) { | ||
let doError = (type === 'error'); | ||
|
||
|
@@ -212,22 +155,13 @@ EventEmitter.prototype.emit = function emit(type, ...args) { | |
needDomainExit = true; | ||
} | ||
|
||
const isFn = typeof handler === 'function'; | ||
switch (args.length) { | ||
case 0: | ||
emitNone(handler, isFn, this); | ||
break; | ||
case 1: | ||
emitOne(handler, isFn, this, args[0]); | ||
break; | ||
case 2: | ||
emitTwo(handler, isFn, this, args[0], args[1]); | ||
break; | ||
case 3: | ||
emitThree(handler, isFn, this, args[0], args[1], args[2]); | ||
break; | ||
default: | ||
emitMany(handler, isFn, this, args); | ||
if (typeof handler === 'function') { | ||
handler.apply(this, args); | ||
} else { | ||
const len = handler.length; | ||
const listeners = arrayClone(handler, len); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you leave a TODO here to consider switching to |
||
for (var i = 0; i < len; ++i) | ||
listeners[i].apply(this, args); | ||
} | ||
|
||
if (needDomainExit) | ||
|
@@ -313,23 +247,11 @@ EventEmitter.prototype.prependListener = | |
return _addListener(this, type, listener, true); | ||
}; | ||
|
||
function onceWrapper() { | ||
function onceWrapper(...args) { | ||
if (!this.fired) { | ||
this.target.removeListener(this.type, this.wrapFn); | ||
this.fired = true; | ||
switch (arguments.length) { | ||
case 0: | ||
return this.listener.call(this.target); | ||
case 1: | ||
return this.listener.call(this.target, arguments[0]); | ||
case 2: | ||
return this.listener.call(this.target, arguments[0], arguments[1]); | ||
case 3: | ||
return this.listener.call(this.target, arguments[0], arguments[1], | ||
arguments[2]); | ||
default: | ||
this.listener.apply(this.target, arguments); | ||
} | ||
this.listener.apply(this.target, args); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coding style question: why not
let
? (Not just here but throughout the patch)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its much slower because it creates a closure for the variable in each loop iteration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mathiasbynens We have a lint rule re: this so that's the main reason. I think @TimothyGu tried to change it recently and feedback from @bmeurer was that we shouldn't quite yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the relevant PR with more conversation: #15648