Performance: JavaScript expressions in templates
This affects rendering speed and responsiveness by how often expressions are evaluated and how complex they are inside Vue templates.
<script setup> import { computed } from 'vue' const activeCount = computed(() => items.filter(item => item.active).length) </script> <template> <div>{{ activeCount }}</div> </template>
<template>
<div>{{ items.filter(item => item.active).length }}</div>
</template>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Complex inline expressions | Multiple recalculations | Multiple reflows if DOM changes | High paint cost due to frequent updates | [X] Bad |
| Computed properties for logic | Minimal recalculations | Single reflow per change | Lower paint cost with fewer updates | [OK] Good |
message?{{ }} to insert JavaScript expressions in templates.message is {{ message }}.{{ }} as normal JavaScript.5 + 3 correctly adds numbers to get 8. {{ '5' + '3' }} concatenates strings, {{ add(5, 3) }} calls an undefined function, and {{ 5 & 3 }} uses bitwise AND.count: 4, what will be displayed by this template snippet?<div>{{ count * 2 }}</div>count * 2 multiplies the value of count (which is 4) by 2.<div>{{ user.name.toUpperCase }}</div>user.name is a string.toUpperCase is a function and must be called with parentheses ().user.name.toUpperCase() to get the uppercase string.numbers = [1, 2, 3]. Which expression correctly creates a comma-separated string of doubled numbers?map with a function n => n * 2.join(', ') converts the array to a comma-separated string for display.Pass the test and this pattern moves unknown