-
Notifications
You must be signed in to change notification settings - Fork 30.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
events: optimize performance of add, remove, emit
If more than one handler is bound to an event type, avoid copying it on emit and instead set a flag that regulates whether to make a copy when a new event is added or removed. Do not use delete or Object.create(null) when removing listeners, instead set to undefined and modify other methods to account for this.
- Loading branch information
1 parent
3a4fe77
commit 7aca9c8
Showing
10 changed files
with
223 additions
and
94 deletions.
There are no files selected for viewing
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
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,36 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
const EventEmitter = require('events').EventEmitter; | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [2e7], | ||
listeners: [1, 5, 10], | ||
}); | ||
|
||
function main(conf) { | ||
var n = conf.n | 0; | ||
const listeners = Math.max(conf.listeners | 0, 1); | ||
|
||
const ee = new EventEmitter(); | ||
|
||
if (listeners === 1) | ||
n *= 5; | ||
else if (listeners === 5) | ||
n *= 2; | ||
|
||
for (var k = 0; k < listeners; k += 1) { | ||
ee.on('dummy', function() {}); | ||
ee.on(`dummy${k}`, function() {}); | ||
} | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
if (i % 3 === 0) | ||
ee.emit('dummy', true, 5); | ||
else if (i % 2 === 0) | ||
ee.emit('dummy', true, 5, 10, false); | ||
else | ||
ee.emit('dummy'); | ||
} | ||
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
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,30 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
const EventEmitter = require('events').EventEmitter; | ||
|
||
const bench = common.createBenchmark(main, { n: [1e6] }); | ||
|
||
function main(conf) { | ||
const n = conf.n | 0; | ||
|
||
const ee = new EventEmitter(); | ||
|
||
for (var k = 0; k < 9; k += 1) { | ||
ee.on(`dummy0${k}`, function() {}); | ||
ee.on(`dummy1${k}`, function() {}); | ||
ee.on(`dummy2${k}`, function() {}); | ||
} | ||
|
||
ee.removeAllListeners('dummy01'); | ||
ee.removeAllListeners('dummy11'); | ||
ee.removeAllListeners('dummy21'); | ||
ee.removeAllListeners('dummy06'); | ||
ee.removeAllListeners('dummy16'); | ||
ee.removeAllListeners('dummy26'); | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
ee.eventNames(); | ||
} | ||
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
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
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
Oops, something went wrong.