Performance: Radio buttons and checkboxes
MEDIUM IMPACT
This affects page responsiveness and rendering speed when many form controls are used or dynamically updated.
<form id="optionsForm"> <input type="checkbox" id="cb1" name="option"> <input type="checkbox" id="cb2" name="option"> <!-- repeated many times --> </form> <script> document.getElementById('optionsForm').addEventListener('change', event => { if(event.target.matches('input[type="checkbox"]')) { requestAnimationFrame(() => { document.body.style.backgroundColor = 'yellow'; }); } }); </script>
<form> <input type="checkbox" id="cb1" name="option" onchange="updateUI()"> <input type="checkbox" id="cb2" name="option" onchange="updateUI()"> <!-- repeated many times --> </form> <script> function updateUI() { document.body.style.backgroundColor = 'yellow'; } </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Individual onchange handlers on many inputs | Many event listeners | Multiple reflows per input | High paint cost due to frequent style changes | [X] Bad |
| Single delegated event listener with batched updates | One event listener | Single reflow per batch | Low paint cost | [OK] Good |