0
0
Bootsrapmarkup~8 mins

Carousel slides and controls in Bootsrap - Performance & Optimization

Choose your learning style9 modes available
Performance: Carousel slides and controls
MEDIUM IMPACT
This affects page load speed and interaction responsiveness by controlling how many DOM elements and event listeners are active during carousel use.
Implementing a carousel with many slides and controls
Bootsrap
<div id="carouselExample" class="carousel slide" data-bs-ride="carousel" data-bs-interval="false">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="img1.jpg" class="d-block w-100" alt="...">
    </div>
    <!-- Load other slides lazily or on demand -->
  </div>
  <button class="carousel-control-prev" type="button" data-bs-target="#carouselExample" data-bs-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="visually-hidden">Previous</span>
  </button>
  <button class="carousel-control-next" type="button" data-bs-target="#carouselExample" data-bs-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="visually-hidden">Next</span>
  </button>
</div>
<script>
  // Lazy load images for non-active slides
  const carousel = document.getElementById('carouselExample');
  carousel.addEventListener('slide.bs.carousel', event => {
    const nextSlide = event.relatedTarget;
    const img = nextSlide.querySelector('img[data-src]');
    if (img && !img.src) {
      img.src = img.dataset.src;
    }
  });
</script>
Only active slide images load immediately; others load on demand, reducing initial DOM size and repaint cost. Controls trigger minimal reflows.
📈 Performance GainSingle reflow per slide change; reduces initial load and memory usage.
Implementing a carousel with many slides and controls
Bootsrap
<div id="carouselExample" class="carousel slide" data-bs-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="img1.jpg" class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item">
      <img src="img2.jpg" class="d-block w-100" alt="...">
    </div>
    <!-- many more slides -->
  </div>
  <button class="carousel-control-prev" type="button" data-bs-target="#carouselExample" data-bs-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="visually-hidden">Previous</span>
  </button>
  <button class="carousel-control-next" type="button" data-bs-target="#carouselExample" data-bs-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="visually-hidden">Next</span>
  </button>
</div>
All slides are loaded and kept in the DOM, causing large DOM size and many repaints when switching slides. Controls trigger full reflows on each click.
📉 Performance CostTriggers multiple reflows and repaints per slide change; large DOM increases initial load time.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
All slides loaded upfront with full controlsHigh (many nodes)Multiple reflows per slide changeHigh paint cost due to many images[X] Bad
Lazy load slides and minimal active DOM nodesLow (few nodes active)Single reflow per slide changeLow paint cost with on-demand images[OK] Good
Rendering Pipeline
Carousel controls trigger style recalculations and layout changes when slides change. Loading many slides upfront increases paint and composite costs.
Style Calculation
Layout
Paint
Composite
⚠️ BottleneckLayout and Paint stages due to large DOM and image loading
Core Web Vital Affected
INP
This affects page load speed and interaction responsiveness by controlling how many DOM elements and event listeners are active during carousel use.
Optimization Tips
1Avoid loading all carousel slides and images upfront to reduce initial load time.
2Use lazy loading for images in non-active slides to minimize paint cost.
3Keep carousel controls lightweight to reduce reflows and improve interaction speed.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance issue with loading all carousel slides upfront?
AIncreases initial page load and causes many reflows on slide change
BPrevents keyboard navigation
CDisables carousel controls
DImproves Largest Contentful Paint
DevTools: Performance
How to check: Open DevTools, go to Performance tab, record while interacting with carousel slides, then analyze Layout and Paint events.
What to look for: Look for multiple Layout and Paint events on each slide change indicating costly reflows and repaints.