Performance: Dynamic attribute names
MEDIUM IMPACT
This affects how Vue updates the DOM attributes dynamically, impacting rendering speed and layout stability.
<template> <input :placeholder="dynamicAttrName === 'placeholder' ? value : ''" :title="dynamicAttrName === 'title' ? value : ''" /> </template> <script setup> import { ref } from 'vue' const dynamicAttrName = ref('placeholder') const value = ref('Enter text') </script>
<template> <input v-bind:[dynamicAttrName]="value" /> </template> <script setup> import { ref } from 'vue' const dynamicAttrName = ref('placeholder') const value = ref('Enter text') </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Dynamic attribute names with v-bind:[attr] | Multiple attribute updates | Triggers 1 reflow per update | Moderate paint cost | [X] Bad |
| Conditional static attribute bindings | Minimal attribute updates | Single reflow on value change | Lower paint cost | [OK] Good |