Skip to content

Commit

Permalink
benchmark: Improve event performance tests.
Browse files Browse the repository at this point in the history
Currently most of the event tests only test a single event type,
which might let those benchmark take fast-paths (in V8) that aren't
taken in realistic use cases, and thus the benchmarks are not good
proxies of real world uses.

PR-URL: #14052
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
  • Loading branch information
bmeurer authored and addaleax committed Jul 18, 2017
1 parent cf6afe3 commit 65a2e80
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
11 changes: 7 additions & 4 deletions benchmark/events/ee-add-remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ function main(conf) {

bench.start();
for (var i = 0; i < n; i += 1) {
for (k = listeners.length; --k >= 0; /* empty */)
ee.on('dummy', listeners[k]);
for (k = listeners.length; --k >= 0; /* empty */)
ee.removeListener('dummy', listeners[k]);
var dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
for (k = listeners.length; --k >= 0; /* empty */) {
ee.on(dummy, listeners[k]);
}
for (k = listeners.length; --k >= 0; /* empty */) {
ee.removeListener(dummy, listeners[k]);
}
}
bench.end(n);
}
9 changes: 6 additions & 3 deletions benchmark/events/ee-listener-count-on-prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ function main(conf) {

var ee = new EventEmitter();

for (var k = 0; k < 10; k += 1)
ee.on('dummy', function() {});
for (var k = 0; k < 5; k += 1) {
ee.on('dummy0', function() {});
ee.on('dummy1', function() {});
}

bench.start();
for (var i = 0; i < n; i += 1) {
ee.listenerCount('dummy');
var dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
ee.listenerCount(dummy);
}
bench.end(n);
}
9 changes: 6 additions & 3 deletions benchmark/events/ee-listeners-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ function main(conf) {
var ee = new EventEmitter();
ee.setMaxListeners(101);

for (var k = 0; k < 100; k += 1)
ee.on('dummy', function() {});
for (var k = 0; k < 50; k += 1) {
ee.on('dummy0', function() {});
ee.on('dummy1', function() {});
}

bench.start();
for (var i = 0; i < n; i += 1) {
ee.listeners('dummy');
var dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
ee.listeners(dummy);
}
bench.end(n);
}
9 changes: 6 additions & 3 deletions benchmark/events/ee-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ function main(conf) {

var ee = new EventEmitter();

for (var k = 0; k < 10; k += 1)
ee.on('dummy', function() {});
for (var k = 0; k < 5; k += 1) {
ee.on('dummy0', function() {});
ee.on('dummy1', function() {});
}

bench.start();
for (var i = 0; i < n; i += 1) {
ee.listeners('dummy');
var dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
ee.listeners(dummy);
}
bench.end(n);
}
5 changes: 3 additions & 2 deletions benchmark/events/ee-once.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ function main(conf) {

bench.start();
for (var i = 0; i < n; i += 1) {
ee.once('dummy', listener);
ee.emit('dummy');
var dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
ee.once(dummy, listener);
ee.emit(dummy);
}
bench.end(n);
}

0 comments on commit 65a2e80

Please sign in to comment.