Performance: Why interactive components need JavaScript
This affects how quickly interactive parts of a page respond and how fast the page becomes usable.
<button class="dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Menu</button> <ul class="dropdown-menu"> <li><a href="#">Item 1</a></li> <li><a href="#">Item 2</a></li> </ul> <script src="bootstrap.bundle.min.js" defer></script>
<button class="dropdown-toggle" data-toggle="dropdown">Menu</button> <ul class="dropdown-menu"> <li><a href="#">Item 1</a></li> <li><a href="#">Item 2</a></li> </ul> <script src="bootstrap.js"></script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Load full Bootstrap JS synchronously | Multiple event listeners added after blocking | Triggers multiple reflows during setup | High paint cost due to delayed interaction | [X] Bad |
| Load Bootstrap JS with defer attribute | Event listeners added after HTML parsing | Single reflow on interaction | Lower paint cost, faster interaction | [OK] Good |
<script src='...'></script> tags, usually before the closing </body> tag for better loading.<script src='bootstrap.js'></script> placed before closing </body> uses <script src='bootstrap.js'></script> before </body>, which is correct. Others misuse <link> or <style> tags or mix CSS and JS.Pass the test and this pattern moves unknown