-
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 avoiding the creation of wrapper functions. These changes bring ~51% increase in performance when simply adding once() event handlers, ~205% increase in the included ee-emit-once benchmark, and a ~36% increase in the included ee-add-remove-once benchmark. This commit also adds new benchmarks, bumps the default number of iterations for several of the existing benchmarks, and also adds missing forced optimizations.
- Loading branch information
Showing
20 changed files
with
871 additions
and
196 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,35 @@ | ||
'use strict'; | ||
|
||
var common = require('../common'); | ||
var EventEmitter = require('events'); | ||
var v8 = require('v8'); | ||
|
||
var bench = common.createBenchmark(main, { | ||
n: [50e6] | ||
}); | ||
|
||
function main(conf) { | ||
var n = +conf.n; | ||
var ee = new EventEmitter(); | ||
var listeners = []; | ||
|
||
var k; | ||
for (k = 0; k < 10; k += 1) | ||
listeners.push(function() {}); | ||
|
||
// Force optimization before starting the benchmark | ||
ee.once('dummy', listeners[0]); | ||
ee._events = {}; | ||
v8.setFlagsFromString('--allow_natives_syntax'); | ||
eval('%OptimizeFunctionOnNextCall(ee.once)'); | ||
ee.once('dummy', listeners[0]); | ||
ee._events = {}; | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; ++i) { | ||
for (k = listeners.length; --k >= 0; /* empty */) | ||
ee.once('dummy', listeners[k]); | ||
ee._events = {}; | ||
} | ||
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,41 @@ | ||
'use strict'; | ||
|
||
var common = require('../common'); | ||
var EventEmitter = require('events'); | ||
var v8 = require('v8'); | ||
|
||
var bench = common.createBenchmark(main, { | ||
n: [25e6] | ||
}); | ||
|
||
function main(conf) { | ||
var n = conf.n | 0; | ||
|
||
var ee = new EventEmitter(); | ||
var listeners = []; | ||
|
||
var k; | ||
for (k = 0; k < 10; k += 1) | ||
listeners.push(function() {}); | ||
|
||
for (k = listeners.length; --k >= 0; /* empty */) | ||
ee.once('dummy', listeners[k]); | ||
for (k = listeners.length; --k >= 0; /* empty */) | ||
ee.removeListener('dummy', listeners[k]); | ||
v8.setFlagsFromString('--allow_natives_syntax'); | ||
eval('%OptimizeFunctionOnNextCall(ee.once)'); | ||
eval('%OptimizeFunctionOnNextCall(ee.removeListener)'); | ||
for (k = listeners.length; --k >= 0; /* empty */) | ||
ee.once('dummy', listeners[k]); | ||
for (k = listeners.length; --k >= 0; /* empty */) | ||
ee.removeListener('dummy', listeners[k]); | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; i += 1) { | ||
for (k = listeners.length; --k >= 0; /* empty */) | ||
ee.once('dummy', listeners[k]); | ||
for (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
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,35 @@ | ||
'use strict'; | ||
|
||
var common = require('../common'); | ||
var EventEmitter = require('events'); | ||
var v8 = require('v8'); | ||
|
||
var bench = common.createBenchmark(main, { | ||
n: [100e5] | ||
}); | ||
|
||
function main(conf) { | ||
var n = +conf.n; | ||
var ee = new EventEmitter(); | ||
var listeners = []; | ||
|
||
var k; | ||
for (k = 0; k < 10; k += 1) | ||
listeners.push(function() {}); | ||
|
||
// Force optimization before starting the benchmark | ||
ee.on('dummy', listeners[0]); | ||
ee._events = {}; | ||
v8.setFlagsFromString('--allow_natives_syntax'); | ||
eval('%OptimizeFunctionOnNextCall(ee.on)'); | ||
ee.on('dummy', listeners[0]); | ||
ee._events = {}; | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; ++i) { | ||
for (k = listeners.length; --k >= 0; /* empty */) | ||
ee.on('dummy', listeners[k]); | ||
ee._events = {}; | ||
} | ||
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,32 @@ | ||
'use strict'; | ||
|
||
var common = require('../common'); | ||
var EventEmitter = require('events'); | ||
var v8 = require('v8'); | ||
|
||
var bench = common.createBenchmark(main, { | ||
n: [25e6] | ||
}); | ||
|
||
function main(conf) { | ||
var n = conf.n | 0; | ||
|
||
var ee = new EventEmitter(); | ||
|
||
function noop() {} | ||
|
||
ee.once('dummy', noop); | ||
ee.emit('dummy'); | ||
v8.setFlagsFromString('--allow_natives_syntax'); | ||
eval('%OptimizeFunctionOnNextCall(ee.once)'); | ||
eval('%OptimizeFunctionOnNextCall(ee.emit)'); | ||
ee.once('dummy', noop); | ||
ee.emit('dummy'); | ||
|
||
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
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.