Performance: Creating custom directives
MEDIUM IMPACT
Custom directives affect how the browser updates and manipulates the DOM, impacting rendering speed and interaction responsiveness.
app.directive('good-style', { updated(el, binding) { const size = binding.value + 'px'; const margin = binding.value / 2 + 'px'; el.style.cssText = `width: ${size}; height: ${size}; margin: ${margin};`; } });
app.directive('bad-style', { updated(el, binding) { el.style.width = binding.value + 'px'; el.style.height = binding.value + 'px'; el.style.margin = binding.value / 2 + 'px'; } });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple separate style changes in directive | Few nodes updated | Multiple reflows per update | Moderate paint cost | [X] Bad |
| Batch style changes with cssText in directive | Few nodes updated | Single reflow per update | Lower paint cost | [OK] Good |