Allow listeners to stop an event's propagation by mike-marcacci · Pull Request #127 · EventEmitter2/EventEmitter2 · GitHub
Skip to content

Allow listeners to stop an event's propagation - #127

Open
mike-marcacci wants to merge 1 commit into
EventEmitter2:masterfrom
mike-marcacci:stopPropagation
Open

mike-marcacci wants to merge 1 commit into
EventEmitter2:masterfrom
mike-marcacci:stopPropagation

Conversation

@mike-marcacci

Copy link
Copy Markdown

While working on #126, I realized that it's currently not possible for listeners to stop the propagation of events, so I added it.

In my implementation, a listener can stop propagation by returning false (compared with ===). Tests and doc updates are included in the PR.

Let me know if you have any thoughts about this either way.

Cheers,
Mike

@heapwolf

Copy link
Copy Markdown
Member

@mike-marcacci

Copy link
Copy Markdown
Author

Ya, sure thing!

Because the emit method returns as soon as propagation is stopped, execution could actually end up being considerably faster when an early listener stops propagation; on the other hand, it does add an extra comparison to each successful if all listeners are processed as before.

To me, it sounds like the current benchmark scripts should be able to cover these changes, unless I’m missing something. I’d be happy to add another benchmark if you can point me in the right direction.

Here is a comparison of the current benchmarks run from both branches:

stopPropagation

EventEmitterHeatUp x 1,416,737 ops/sec ±0.71% (100 runs sampled)
EventEmitter x 1,532,806 ops/sec ±0.46% (98 runs sampled)
EventEmitter2 x 9,411,991 ops/sec ±0.56% (98 runs sampled)
EventEmitter2 (wild) x 6,423,381 ops/sec ±9.22% (93 runs sampled)

EventEmitterHeatUp x 1,294,572 ops/sec ±0.45% (100 runs sampled)
EventEmitter x 1,475,587 ops/sec ±0.63% (97 runs sampled)
EventEmitter2 x 8,797,374 ops/sec ±0.47% (98 runs sampled)
EventEmitter2 (wild) x 6,255,054 ops/sec ±9.13% (95 runs sampled)

EventEmitterHeatUp x 1,430,530 ops/sec ±0.56% (99 runs sampled)
EventEmitter x 1,397,150 ops/sec ±0.52% (97 runs sampled)
EventEmitter2 x 8,770,814 ops/sec ±0.45% (99 runs sampled)
EventEmitter2 (wild) x 4,457,747 ops/sec ±5.88% (96 runs sampled)

EventEmitterHeatUp x 1,420,539 ops/sec ±0.71% (98 runs sampled)
EventEmitter x 1,502,386 ops/sec ±0.70% (98 runs sampled)
EventEmitter2 x 9,216,787 ops/sec ±0.40% (96 runs sampled)
EventEmitter2 (wild) x 6,311,002 ops/sec ±9.28% (98 runs sampled)

master

EventEmitterHeatUp x 1,416,757 ops/sec ±0.76% (96 runs sampled)
EventEmitter x 1,538,006 ops/sec ±0.42% (99 runs sampled)
EventEmitter2 x 9,082,098 ops/sec ±0.52% (99 runs sampled)
EventEmitter2 (wild) x 6,159,633 ops/sec ±9.24% (96 runs sampled)

EventEmitterHeatUp x 1,382,359 ops/sec ±0.31% (99 runs sampled)
EventEmitter x 1,355,995 ops/sec ±0.58% (101 runs sampled)
EventEmitter2 x 9,289,798 ops/sec ±0.42% (99 runs sampled)
EventEmitter2 (wild) x 6,560,096 ops/sec ±9.21% (92 runs sampled)

EventEmitterHeatUp x 1,407,961 ops/sec ±1.34% (99 runs sampled)
EventEmitter x 1,525,306 ops/sec ±0.49% (102 runs sampled)
EventEmitter2 x 9,093,574 ops/sec ±0.48% (94 runs sampled)
EventEmitter2 (wild) x 6,156,239 ops/sec ±9.19% (92 runs sampled)

EventEmitterHeatUp x 1,273,150 ops/sec ±6.06% (87 runs sampled)
EventEmitter x 1,479,227 ops/sec ±0.85% (100 runs sampled)
EventEmitter2 x 8,806,507 ops/sec ±0.70% (94 runs sampled)
EventEmitter2 (wild) x 5,237,295 ops/sec ±8.16% (91 runs sampled)

@RangerMauve

Copy link
Copy Markdown
Contributor

@0x00A Do the bechmarks look good enough to you?

@elmatou

elmatou commented Nov 8, 2019

Copy link
Copy Markdown

This would be a nice addition.

@RangerMauve

Copy link
Copy Markdown
Contributor

zgover added a commit to zgover/EventEmitter2 that referenced this pull request Aug 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants