Performance: Directive with arguments and modifiers
MEDIUM IMPACT
This affects how Vue processes directives during rendering, impacting the number of DOM updates and reflows triggered by directive logic.
app.directive('focus', { mounted(el, binding) { const { arg, modifiers } = binding; if (arg === 'input' && modifiers.lazy) { const handler = () => el.focus(); el.addEventListener('change', handler, { once: true }); } else if (arg === 'button' && modifiers.immediate) { el.focus(); } } });
app.directive('focus', { mounted(el, binding) { if (binding.arg === 'input' && binding.modifiers.lazy) { el.addEventListener('change', () => { el.focus(); }); } else if (binding.arg === 'button' && binding.modifiers.immediate) { el.focus(); } } });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Directive with multiple modifiers and dynamic arguments causing repeated event listeners | Multiple event listeners per element | Multiple reflows per event | High paint cost due to frequent focus calls | [X] Bad |
| Directive with optimized event listener usage and conditional logic | Single event listener per element | Single reflow per event | Lower paint cost with limited focus calls | [OK] Good |