Somnium
Somnium

Reputation: 1155

What is better for performance: event for each element or one event with delegation?

I need to make big table (for example, 100x100) with input elements in each cell. What will be better for performance: to create event for each input element or to create event for table and use event delegation (propagation)?

I feel like a lot of events will slow down everything, but will delegation be better? I am thinking also about something like creating and removing events dynamically for only visible elements or similar.

Upvotes: 3

Views: 254

Answers (1)

koninos
koninos

Reputation: 5337

If you attach an event handler to the table, you can get the event target, which will contain the element that was actually triggered the event. It will have much less performance impact than applying multiple events.

Note that not all events bubble, so this technique doesn’t work with all event types.

Upvotes: 2

Related Questions