Rule Definition
When Vue processes directives, v-for has a higher priority than v-if. So even if we only render elements for a small fraction, we have to iterate over the entire list every time we re-render, whether or not the set of elements and their relevant v-if props have changed.
This makes rendering inefficient.
Remediation
Use a computed property, that satisfies the v-if condition, to iterate over
Violation Code Sample
Users def:
<ul>
<li
v-for="user in users"
v-if="user.isActive"
:key="user.id"
>
{{ user.name }}
</li>
</ul>
_____________________________________
<TodoItem
v-if="complete"
v-for="todo in todos"
:todo="todo"
/>
Fixed Code Sample
//Evaluate a computed property:
computed: {
activeUsers: function () {
return this.users.filter(function (user) {
return user.isActive
})
}
}
//Iterate
<ul>
<li
v-for="user in activeUsers"
:key="user.id"
>
{{ user.name }}
</li>
</ul>
Reference
https://vuejs.org/v2/style-guide/#Avoid-v-if-with-v-for-essential
CISQ Rule: ASCPEM-PRF-08
Related Technologies
Technical Criterion
CWE-1050 - Excessive Platform Resource Consumption within a Loop
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.