Skip to content

performance,events: use Map to store events in EventEmitter

I've had some time to play around with Map as backing storage for EE and the results turned out to be pretty good. I'm not sure if this is an appropriate change so feel free to note this and close.

If this is acceptable then this should probably be a semver-major, even though _events is 'private'.

EE benchmarks
 ➔ dev/node/node cat compare-events-pr.csv | Rscript benchmark/compare.R        59s events-perf :: ● :: ⬡
                                                     confidence improvement accuracy (*)    (**)   (***)
 events/ee-add-remove.js n=1000000                          ***     11.29 %       ±5.75%  ±7.70% ±10.14%
 events/ee-emit.js listeners=1 argc=0 n=2000000             ***     16.70 %       ±8.86% ±11.89% ±15.66%
 events/ee-emit.js listeners=1 argc=10 n=2000000                     8.69 %      ±10.88% ±14.58% ±19.18%
 events/ee-emit.js listeners=1 argc=2 n=2000000                      5.96 %       ±7.76% ±10.41% ±13.73%
 events/ee-emit.js listeners=1 argc=4 n=2000000              **     10.72 %       ±7.08%  ±9.49% ±12.51%
 events/ee-emit.js listeners=10 argc=0 n=2000000                     2.64 %      ±10.67% ±14.30% ±18.80%
 events/ee-emit.js listeners=10 argc=10 n=2000000                    1.67 %       ±7.19%  ±9.63% ±12.67%
 events/ee-emit.js listeners=10 argc=2 n=2000000                    -0.33 %       ±5.85%  ±7.85% ±10.36%
 events/ee-emit.js listeners=10 argc=4 n=2000000                    -4.62 %       ±6.34%  ±8.51% ±11.24%
 events/ee-emit.js listeners=5 argc=0 n=2000000                     -2.17 %       ±9.37% ±12.56% ±16.53%
 events/ee-emit.js listeners=5 argc=10 n=2000000                    -0.05 %       ±8.56% ±11.48% ±15.11%
 events/ee-emit.js listeners=5 argc=2 n=2000000                     -0.02 %       ±8.38% ±11.24% ±14.80%
 events/ee-emit.js listeners=5 argc=4 n=2000000                      4.58 %       ±6.30%  ±8.45% ±11.15%
 events/ee-listener-count-on-prototype.js n=50000000        ***     22.49 %       ±8.90% ±12.00% ±15.95%
 events/ee-listeners-many.js n=5000000                        *      9.11 %       ±7.27%  ±9.80% ±13.02%
 events/ee-listeners.js n=5000000                                   -3.90 %       ±8.99% ±12.04% ±15.84%
 events/ee-once.js n=20000000                                        0.76 %       ±4.47%  ±5.99%  ±7.87%

Also I'd like some help on understanding why is .clear() is so much worse than creating a new Map, I thought that this should be the other way around. (it shows regression of around -25% for ee-once). Comments with _events.clear() are there to show where I intended to use it. I'll remove them later.

Perf with new Map image Perf with map.clear() image

Perf of master if needed: image

Checklist
  • make -j4 test (UNIX) passes
  • benchmarks are included
  • documentation is changed or added (there is single mention in events.md in the console log of EE)
  • commit message follows commit guidelines

/cc @nodejs/performance

Merge request reports

Loading