0
0
Vueframework~8 mins

Dynamic attribute names in Vue - Performance & Optimization

Choose your learning style9 modes available
Performance: Dynamic attribute names
MEDIUM IMPACT
This affects how Vue updates the DOM attributes dynamically, impacting rendering speed and layout stability.
Setting HTML attributes dynamically based on component state
Vue
<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>
Using conditional static attribute bindings avoids changing attribute names dynamically, reducing DOM updates and layout shifts.
📈 Performance GainSingle reflow only when value changes, no reflow caused by attribute name changes, reducing CLS.
Setting HTML attributes dynamically based on component state
Vue
<template>
  <input v-bind:[dynamicAttrName]="value" />
</template>

<script setup>
import { ref } from 'vue'
const dynamicAttrName = ref('placeholder')
const value = ref('Enter text')
</script>
Binding dynamic attribute names directly causes Vue to update the DOM attribute every time the reactive value changes, triggering layout recalculations.
📉 Performance CostTriggers 1 reflow per attribute update, causing layout shifts and increased CLS.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Dynamic attribute names with v-bind:[attr]Multiple attribute updatesTriggers 1 reflow per updateModerate paint cost[X] Bad
Conditional static attribute bindingsMinimal attribute updatesSingle reflow on value changeLower paint cost[OK] Good
Rendering Pipeline
When Vue updates dynamic attribute names, it triggers Style Calculation and Layout stages because the browser must recalculate styles and layout for changed attributes. Paint and Composite stages follow to render the updated element.
Style Calculation
Layout
Paint
Composite
⚠️ BottleneckLayout is most expensive because changing attribute names can cause layout recalculations.
Core Web Vital Affected
CLS
This affects how Vue updates the DOM attributes dynamically, impacting rendering speed and layout stability.
Optimization Tips
1Avoid binding attribute names dynamically with v-bind:[attr].
2Use conditional static attribute bindings to control attribute values.
3Monitor layout shifts in DevTools Performance panel to catch CLS issues.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance cost of using dynamic attribute names in Vue?
ATriggers layout recalculations causing reflows
BBlocks JavaScript execution for long periods
CIncreases network requests for assets
DCauses memory leaks in the browser
DevTools: Performance
How to check: Record a performance profile while interacting with the component. Look for Layout events triggered by attribute changes.
What to look for: Frequent Layout events and long Layout durations indicate costly dynamic attribute updates.