0
0
Bootsrapmarkup~8 mins

Carousel indicators in Bootsrap - Performance & Optimization

Choose your learning style9 modes available
Performance: Carousel indicators
MEDIUM IMPACT
Carousel indicators affect the interaction responsiveness and visual stability during slide changes in a carousel component.
Updating carousel indicators on slide change
Bootsrap
<script>const indicators = document.querySelector('.carousel-indicators'); indicators.addEventListener('click', e => { if(e.target.tagName === 'BUTTON'){ indicators.querySelector('.active')?.classList.remove('active'); e.target.classList.add('active'); } });</script>
Using event delegation reduces event listeners and limits DOM writes to only two class changes per click.
📈 Performance Gainsingle reflow per click regardless of number of indicators
Updating carousel indicators on slide change
Bootsrap
<script>document.querySelectorAll('.carousel-indicators button').forEach(btn => btn.addEventListener('click', () => { document.querySelectorAll('.carousel-indicators button').forEach(b => b.classList.remove('active')); btn.classList.add('active'); }));</script>
This code triggers multiple DOM writes and forces layout recalculations on every click, causing slow interaction on many indicators.
📉 Performance Costtriggers N reflows per click where N is number of indicators
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Multiple event listeners on each indicatorMany event listeners, multiple class changesN reflows per clickHigh paint cost due to frequent style changes[X] Bad
Single event listener with event delegationOne event listener, two class changes per click1 reflow per clickLow paint cost, minimal style recalculations[OK] Good
Rendering Pipeline
Carousel indicators update triggers style recalculation and layout changes when active states toggle, affecting paint and composite stages.
Style Calculation
Layout
Paint
Composite
⚠️ BottleneckLayout due to multiple class changes causing reflows
Core Web Vital Affected
INP
Carousel indicators affect the interaction responsiveness and visual stability during slide changes in a carousel component.
Optimization Tips
1Use event delegation to minimize event listeners on carousel indicators.
2Limit DOM writes to only necessary class changes when updating active states.
3Avoid triggering multiple reflows by batching style changes.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance problem with adding separate click listeners to each carousel indicator button?
AIt causes multiple reflows and slows down interaction as the number of indicators grows.
BIt increases the initial page load time significantly.
CIt causes the carousel images to load slower.
DIt prevents the carousel from being accessible.
DevTools: Performance
How to check: Record a performance profile while clicking carousel indicators. Look for layout and style recalculation events in the flame chart.
What to look for: Multiple layout events per click indicate inefficient DOM updates; fewer layout events show optimized performance.