Morglod / tseep

Fastest event emitter in the world for js (and only 381 bytes in build)
https://github.com/Morglod/tseep/blob/master/benchmarks/README.md
MIT License
173 stars 5 forks source link

Performance with a setup with frequently changing listeners #22

Open laurisvan opened 1 month ago

laurisvan commented 1 month ago

Hi,

I am pondering on how to tune my EventEmitter setup. In our case, we use EventEmitter for internal message queue where the clients frequently subscribe and unsubscribe from the listener. It might be that they will receive only a few events prior to unsubscribing.

Due to the internal eval/compile nature of tseep, I was wondering on whether it is suitable for such a situation - it might be that the recompilation of the listeners eat the performance gains of the fast emitting.

Do you have a good rule of thumb or a benchmark of a ratio between emits and listens on when adding/removing a listener becomes a burden?

Morglod commented 1 month ago

Hi!

I thing it goes directly to how frequent is "frequently" and how much events you have, also you should test all together on user platform because of JIT nature of JS engines.

According to this bench, for 10 listeners:

classic array init + first call: 0.1ms, then 0.06ms per call baked array + first call: 0.6ms, then 0.006ms per call

(0.6-0.1)/(0.06-0.006), after 8 emits you will have benefit of (0.06-0.006)=0.054ms per call if compilation will go sync way. (probably after first call, you will have some "timeout" before next call, so first call will not affect you at all.

But its only talking about "compilation" method. tseep is highly optimized on hotpath calls, unlike other event emitters, so speedup may be higher, like 3-4 emits.

For example tseep skips baking for 1 listener and swap implementation for different cases, so eliminates unreachable or not necessary code at all.

Also if you have fixed number of arguments in listeners, better specify argsNum argument in addListener function, and keep them not greater than 5


I test this case some time ago and tseep was x1.5-x3 performance than other event emitters even with listeners recompilation.

Fast answer: with synthetic test, for 10 listeners, it should be 8 emits without any timeouts, after you will have benefits.

But you should test it all together, probably try different event emitters and measure rps, otherwise this numbers are highly theoretical because JIT and async stuff.

Will be cool if you share your benchmarks when you test it.

laurisvan commented 1 month ago

Thanks for the reply! I will try this out in the coming days and get back to you. Sounds promising!