Performance: Template refs for DOM access
MEDIUM IMPACT
Accessing DOM elements via template refs affects interaction responsiveness and can impact layout recalculations if used inefficiently.
<template> <button @click="updateStyle">Click me</button> <div ref="box">Box</div> </template> <script setup> import { ref } from 'vue' const box = ref(null) function updateStyle() { let newWidth = 100 // Calculate final width once newWidth += 100 box.value.style.width = `${newWidth}px` } </script>
<template> <button @click="updateStyle">Click me</button> <div ref="box">Box</div> </template> <script setup> import { ref } from 'vue' const box = ref(null) function updateStyle() { for (let i = 0; i < 100; i++) { box.value.style.width = `${100 + i}px` } } </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple style updates in loop via ref | Many writes | 100 reflows | High paint cost | [X] Bad |
| Single style update via ref | One write | 1 reflow | Low paint cost | [OK] Good |