Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reasonable to publish thousands of different events on an EventEmitter? (benchmarks added) #4030

Closed
iambumblehead opened this issue Nov 16, 2022 · 2 comments
Labels

Comments

@iambumblehead
Copy link

iambumblehead commented Nov 16, 2022

Details

Is it reasonable to use EventEmitter for publishing hundreds or thousands of different events to consumers, or is necessary to broadcast such events through another mechanism. Pre-existing performance benchmarks weren't found.

Following the benchmark guide, and a comment from someone in the PR linked below, I've made the following benchmark script but am not certain how to interpret the results --it would seem using "only" tens of event emitters is a terrible idea. Would someone with benchmark or EventEmitter experience confirm this interpretation?

benchmark script, with results as follows below
const bench = common.createBenchmark(data => {
  const ee = new EventEmitter();
  ee.setMaxListeners(Infinity);
  const listeners = [];

  for (let k = 0; k < 10; k += 1)
    listeners.push(() => {});

  const eventNames = [];

  for (let k = 0; k < data.events; ++k)
    eventNames.push(`dummy${k}`);

  // listen, emit then remove n events
  bench.start();
  for (let i = 0; i < data.n; i += 1) {
    for (const eventName of eventNames) {
      for (let k = listeners.length; k--;)
        ee.on(eventName, listeners[k]);
    }

    for (const eventName of eventNames) {
      for (let k = listeners.length; k--;)
        ee.emit(eventName, data);
    }
    
    for (const eventName of eventNames) {
      for (let k = listeners.length; k--;)
        ee.removeListener(eventName, listeners[k]);
    }
  }
  bench.end(data.n);
}, {
  events: [ 6, ...Array.from(Array(16).keys()).slice(1).map(n => n * 10) ],
  n: [10]
});
t-emitter-benchmarks.js n=10 events=6: 6,518.042266896884
t-emitter-benchmarks.js n=10 events=10: 3,229.8886140612553
t-emitter-benchmarks.js n=10 events=20: 1,635.3395569995967
t-emitter-benchmarks.js n=10 events=30: 1,412.2709967272033
t-emitter-benchmarks.js n=10 events=40: 1,185.9759763962315
t-emitter-benchmarks.js n=10 events=50: 1,118.3136724470437
t-emitter-benchmarks.js n=10 events=60: 937.9777237794424
t-emitter-benchmarks.js n=10 events=70: 919.3701358424538
t-emitter-benchmarks.js n=10 events=80: 853.8230224669013
t-emitter-benchmarks.js n=10 events=90: 811.9099393771205
t-emitter-benchmarks.js n=10 events=100: 726.7387460689794
t-emitter-benchmarks.js n=10 events=110: 676.583211480697
t-emitter-benchmarks.js n=10 events=120: 700.618744437963
t-emitter-benchmarks.js n=10 events=130: 676.4745555595994
t-emitter-benchmarks.js n=10 events=140: 660.326519578054
t-emitter-benchmarks.js n=10 events=150: 623.2406306522083

Node.js version

node 16 latest

Example code

const emitter = new events.EventEmitter()

events.on(emitter, 'one.of.thousands.of.uniquely.named.events')

Operating system

alpine linux

Scope

runtime

Module and version

Not applicable.

@iambumblehead iambumblehead changed the title do benchmarks exist for using events.EventEmitter? reasonable to use thousands of events.EventEmitter consumers? (benchmarks added) Nov 17, 2022
@iambumblehead iambumblehead changed the title reasonable to use thousands of events.EventEmitter consumers? (benchmarks added) reasonable to listen for thousands of different events on an EventEmitter? (benchmarks added) Nov 17, 2022
@iambumblehead iambumblehead changed the title reasonable to listen for thousands of different events on an EventEmitter? (benchmarks added) reasonable to publish thousands of different events on an EventEmitter? (benchmarks added) Nov 17, 2022
@github-actions
Copy link

There has been no activity on this issue for 11 months. The help repository works best when sustained engagement moves conversation forward. The issue will be closed in 1 month. If you are still experiencing this issue on the latest supported versions of Node.js, please leave a comment.

@github-actions github-actions bot added the stale label Oct 26, 2023
Copy link

Closing after no activity on this issue for 12 months.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant