Performance: Text interpolation with mustache syntax
This affects the rendering speed of dynamic text content in the browser and how often the DOM updates when data changes.
<template>
<div>{{ result }}</div>
</template>
<script setup>
import { ref, computed } from 'vue'
const data = ref(10)
const result = computed(() => {
// heavy computation only runs when data changes
return `Result: ${data.value}`;
})
</script><template>
<div>{{ complexFunction() }}</div>
</template>
<script setup>
function complexFunction() {
// heavy computation or side effects
return 'Result';
}
</script>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct function call in mustache | Updates text node every render | Triggers reflow each update | Paints text changes frequently | [X] Bad |
| Computed property in mustache | Updates text node only on data change | Minimal reflows triggered | Paints only when necessary | [OK] Good |
{{ message }} do in a Vue template?{{ }} is used in Vue to insert data values into HTML templates.{{ message }} doesmessage in the rendered HTML.message inside the HTML. -> Option Atitle inside a Vue template using mustache syntax?{{ }} to insert variables into templates.{{ title }} matches Vue's mustache syntax for interpolation.{ name: 'Anna' }, what will be rendered by this template? <div>Hello, {{ name }}!</div>name has the value 'Anna'.{{ name }} with 'Anna' in the rendered HTML.<div>{{ user.name </div>}}.{{ user.name }} with both opening and closing double braces.hour is less than 12, otherwise 'Good afternoon'?<div>{{ hour < 12 ? 'Good morning' : 'Good afternoon' }}</div> uses a valid ternary expression with quotes around strings, which is correct. <div>{{ if (hour < 12) 'Good morning' else 'Good afternoon' }}</div> uses invalid syntax inside mustache. <div>{{ hour < 12 && Good morning || 'Good afternoon' }}</div> misses quotes around Good morning, causing a syntax error. <div>{{ hour < 12 ? Good morning : Good afternoon }}</div> misses quotes around strings, causing errors.Pass the test and this pattern moves unknown