Without Delegation
Without Delegation
Section titled “Without Delegation”Introduction
Section titled “Introduction”Without delegation, you attach an event listener to each individual element — which causes memory and performance issues.
The Problem
Section titled “The Problem”// ❌ Attaching listener to each itemdocument.querySelectorAll('.item').forEach(item => { item.addEventListener('click', () => { console.log('Item clicked:', item.textContent); });});Issues
Section titled “Issues”- Memory overhead — each listener uses memory
- Performance — attaching many listeners is slow
- Dynamic elements — new elements added later won’t have listeners
- Maintenance — harder to remove or update handlers
Visual Explanation
Section titled “Visual Explanation”flowchart LR subgraph "Without Delegation ❌" A["1000 list items"] B["1000 event listeners"] endSummary
Section titled “Summary”- Each listener consumes memory
- Doesn’t work for dynamically added elements
- Harder to maintain many individual listeners
- Event delegation solves all these problems