Event Aggregator Performace Query


#1

Hi all, I have a query in regard to the best practices surrounding the event aggregator. I have many instances of a component that are currently subscribed to an event , that is published throughout my application, currently I am passing a unique id with my event , and performing a check on each subscription to see if the component’s id matches e.g.
this.ea.subscribe( “item:delete:value”, data =>
{
if ( this.item.id== data.id)
{
//do something
}
} );
Needless to say this approach is sluggish if there are a large number of items that are subscribed to the event.
However I am wondering if it would make more sense to fire events with the id in the event name as opposed to in the data that is passed, so something like:
this.ea.subscribe( item:delete:value:${id}, data =>
{
//do something
} );
This would remove the need to have a check at all, but if there are hundreds of items subscribed, and each subscribed to a unique event. My question is basically, is this a recommended approach/ a correct use of Aurelias event aggregator, or is this likely to cause performance issues?
Apologies if there are issues with formatting on this post, or if this is in the incorrect category, this is my first time posting here.
Any help would greatly appreciated! :grinning:


#2

Can you give us more details about your use case, as in why you need to have hundreds of individual subscription?
What I have always done is to use a Service (singleton) to keep my items and use only 1 subscription in the service. I do have to check eventData.id against each item.id but it has never been too terrible for me.
Anyway, with my approach, even if the loop-checking is terrible, at least it’s my code and I can optimize it however I want (like building a Map of [id => item] upfront instead of just an Array of items for example). It’s not code in the EventAggregator class that would be harder to optimize.