Skip to content

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

@iambumblehead

Description

@iambumblehead

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions