-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
events: improve eventEmitter.once() performance
This commit improves once() performance by storing the event handler directly instead of creating a wrapper function every time. These changes bring ~150% increase in performance when simply adding once() event handlers, ~220% increase in the included ee-emit-once benchmark, and a ~50% increase in the included ee-add-remove-once benchmark.
- Loading branch information
Showing
9 changed files
with
376 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var common = require('../common.js'); | ||
var EventEmitter = require('events').EventEmitter; | ||
|
||
var bench = common.createBenchmark(main, {n: [25e6]}); | ||
|
||
function main(conf) { | ||
var n = conf.n | 0; | ||
|
||
var ee = new 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.once('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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var common = require('../common.js'); | ||
var EventEmitter = require('events').EventEmitter; | ||
|
||
var bench = common.createBenchmark(main, {n: [25e6]}); | ||
|
||
function main(conf) { | ||
var n = conf.n | 0; | ||
|
||
var ee = new EventEmitter(); | ||
|
||
function noop() {} | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
ee.once('dummy', noop); | ||
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
Oops, something went wrong.