I have a timeline chart with hundreds of bars displayed on the screen. For each bar I want to show a personalized tooltip when hovering, display a personalized context menu on right-click, select the bar on click and there are other events that need to be handled.
I am wondering what is the correct approach in handling the events, especially keeping in mind performance of the app.
- If I have handlers for each bar (mouseover, mouse leave, mouse click, context menu, etc.) it is easier to implement the logic but I am not sure if there is a performance hit of having subscribed to thousands of events. Obviously I'll be using one mouseover callback function for all mouseover events, one mouse leave callback for all mouse leave events and so on.
- If I have handlers for the chart (mouseover, mouse leave, mouse click, context menu, etc.) and find out in these handlers which is the target bar, it is a more complex implementation, more chances of introducing bugs, but only a few (less than 10) event subscriptions in the app.
I think that the browser triggers the events on any element regardless if there is a subscription for it or not. If that is the case, having a ton of event handlers that might never be invoked should not affect the app's performance.