-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: micro-optimize EventEmitter#removeListener()
Replace the call to Array#splice() with a faster open-coded version that creates less garbage. Add a new benchmark to prove it. With the change applied, it scores a whopping 40% higher. PR-URL: #185 Reviewed-By: Chris Dickinson <[email protected]>
- Loading branch information
1 parent
d0c238c
commit d3f8db1
Showing
2 changed files
with
31 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var common = require('../common.js'); | ||
var events = require('events'); | ||
|
||
var bench = common.createBenchmark(main, {n: [25e4]}); | ||
|
||
function main(conf) { | ||
var n = conf.n | 0; | ||
|
||
var ee = new events.EventEmitter(); | ||
var listeners = []; | ||
|
||
for (var k = 0; k < 10; k += 1) | ||
listeners.push(function() {}); | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
for (var k = listeners.length; --k >= 0; /* empty */) | ||
ee.on('dummy', listeners[k]); | ||
for (var k = listeners.length; --k >= 0; /* empty */) | ||
ee.removeListener('dummy', listeners[k]); | ||
} | ||
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