Performance: Custom refs with customRef
MEDIUM IMPACT
This affects how reactive state updates trigger DOM updates and component re-renders in Vue applications.
import { customRef } from 'vue'; function useDebouncedRef(value, delay = 200) { let timeout; return customRef((track, trigger) => ({ get() { track(); return value; }, set(newValue) { clearTimeout(timeout); timeout = setTimeout(() => { value = newValue; trigger(); }, delay); } })); } const count = useDebouncedRef(0);
import { ref } from 'vue'; const count = ref(0); function increment() { count.value++; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Standard ref with immediate updates | Many reactive triggers | 1 reflow per update | High paint cost on frequent changes | [X] Bad |
| customRef with debounced updates | Fewer reactive triggers | Single reflow per batch | Lower paint cost due to fewer updates | [OK] Good |