Is if statement necessary for empty array situation?

Viewed 887

I'm using Shopify Liquid. If I don't include {% if my_array %} and have a code such as this:

{% for var in my_array %}
Do this heavy task
{% endif %}

does it skip the "heavy task" if my_array is empty or is it better to include the if statement (performance wise)? Thank you.

3 Answers

Thank you for your answers, but I found this from Shopify developers documents. According to Shopify, {% for var in my_array %} also acts like an if statement, which can be combined with {% else %} for when the array is empty. for example:

{% for var in my_array %}
Do this heavy task
{% else %}
<p>This array is empty</p>
{% endfor %}

Hope this helps others searching for it too.

There shouldn't be much of a difference. You may just keep {% if my_array %}.

Since you're writing it in liquid, you won't see any performance issues since the result will already be written when the page loads.

Related