Performance: Prop types and validation
This affects the initial rendering speed and runtime performance by adding checks when components receive props.
<script setup> const props = defineProps({ title: { type: String, required: true }, count: { type: Number, default: 0 } }) </script>
<script setup> const props = defineProps({}) // No prop validation </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No prop validation | No extra DOM ops | 0 | 0 | [OK] Good for raw speed but risky |
| Basic prop types with required/default | No extra DOM ops | 0 | 0 | [!] OK with minor runtime cost |
| Heavy custom validators | No extra DOM ops | 0 | 0 | [X] Bad due to blocking JS execution |
props with types in a Vue component?title in a Vue component?type and required.{ type: String, required: true } inside the prop definition.props: { count: { type: Number, default: 0 } }count inside the component if the parent does NOT pass any count prop?count0, so count will be 0 inside the component.props: { age: { type: Number, required: true } }age="twenty" (a string) instead of a number?status that only accepts the strings 'active', 'inactive', or 'pending'. Which is the correct way to add this validation in Vue?validator function to check if a prop value is allowed.value => ['active', 'inactive', 'pending'].includes(value).Pass the test and this pattern moves unknown