Performance: Control structures (if, for)
MEDIUM IMPACT
Control structures in Flask templates affect how much HTML is generated and how fast the page renders.
{% for item in active_items %}
<div class="active">{{ item.name }}</div>
{% endfor %}
{% for item in inactive_items %}
<div>{{ item.name }}</div>
{% endfor %}{% for item in items %}
{% if item.is_active %}
<div class="active">{{ item.name }}</div>
{% else %}
<div>{{ item.name }}</div>
{% endif %}
{% endfor %}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Complex if inside for loop | Many conditional checks per item | Multiple reflows if DOM changes dynamically | Higher paint cost due to larger HTML | [X] Bad |
| Separate loops with filtered data | No conditional checks | Stable DOM structure | Lower paint cost with simpler HTML | [OK] Good |