Answers.Body: <p>Template performance isn't a huge worry in Craft thanks to the built-in <a href="http://buildwithcraft.com/docs/templating/tags#cache">cache tag</a>. Even if some code is taking time to process, it can be wrapped in that tag to greatly improve performance.</p>
  
  <p>But to more thoroughly answer your question, the scenarios where that tag might have to be used generally revolve around the number of DB queries. Craft's relationship engine it's pretty great, but it can lead to templates making many DB calls. Sometime's they're unavoidable but you can squeeze a bit more performance out.</p>
  
  <p>In particular, the <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel">ElementCriteriaModel</a> (used to retrieve entries, assets, users etc) has some helpful, performance-enhancing methods. For example, instead of retrieving all entries for a section and then only selecting the first one, you can use the <code>.first()</code> method.</p>
  
  <pre><code>{# Fetches all entries #}
  {% set firstEntry = craft.entries.section('yourSection').find()[0] %}
  
  {# Only fetches first entry #}
  {% set firstEntry = craft.entries.section('yourSection').first() %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-30T23:27:03.723Z
Answers.Text: Just be careful that you understand the `{% cache %}` tag correctly. By default it generates a unique cache item for each URL that uses the block, and for large sites this can actually slow down your site over time. See https://craftcms.stackexchange.com/a/22642/5437
Answers.UserDisplayName: Simon East
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-01T15:07:34.327Z
Answers.Text: These days, `first()` is deprecated. Use `one()` instead.
Answers.UserDisplayName: InanisAtheos
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-12T14:09:22.407Z
Answers.LastActivityDate: 2014-06-12T14:09:22.407Z
Answers.UserDisplayName: Stuart Whitehead
Answers.Body: <p>Avoiding unnecessary DB queries is definitely the main thing to watch out for. Here’s an example that came up recently in support: Someone wanted to display the first 6 Matrix blocks in an entry, outputting them 2 at a time. Initially the template code looked something like this:</p>
  
  <pre><code>{% for block in entry.matrixField.limit(2) %}
      &lt;!-- Get the 1st two items --&gt;
  {% endfor %}
  
  {% for block in entry.matrixField.offset(2).limit(2) %}
      &lt;!-- Get the next two items --&gt;
  {% endfor %}
  
  {% for block in entry.matrixField.offset(4).limit(2) %}
      &lt;!-- Get whatever's left, not more than 2 --&gt;
  {% endfor %}
  </code></pre>
  
  <p>Rather than doing that, we suggested that all 6 blocks be grabbed up front, and output 2 at a time using Twig’s <a href="http://twig.sensiolabs.org/doc/filters/slice.html">slice filter</a>:</p>
  
  <pre><code>{% set blocks = entry.matrixField.limit(6) %}
  
  {% for block in blocks|slice(0, 2) %}
      &lt;!-- Get the 1st two items --&gt;
  {% endfor %}
  
  {% for block in blocks|slice(2, 2) %}
      &lt;!-- Get the next two items --&gt;
  {% endfor %}
  
  {% for block in blocks|slice(4, 2) %}
      &lt;!-- Get whatever's left, not more than 2 --&gt;
  {% endfor %}
  </code></pre>
  
  <p>Remember that you can always see what DB queries are happening in a given request by enabling <a href="http://buildwithcraft.com/docs/config-settings#devMode">Dev Mode</a> and checking your browser’s Javascript console, where all DB queries for the current request will be logged, ordered by how long they took. It will also show if the exact same query was run multiple times.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-10T13:34:45.380Z
Answers.Text: Regarding query reduction, I also recently discovered eager loading related elements. https://craftcms.com/docs/templating/eager-loading-elements
Answers.UserDisplayName: mjr
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-12T15:05:18.067Z
Answers.LastActivityDate: 2014-06-12T21:18:06.757Z
Answers.LastEditDate: 2014-06-12T21:18:06.757Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I'm coming to Craft as someone experienced with Expression Engine templates. When I'm coding for EE I try to avoid certain big ticket performance hits, for example:</p>
  
  <p>Using embeds excessively.
  Using the disable parameter.
  Using simple conditionals vs. advanced conditional when possible.
  Keeping a close eye on the query count.
  Etc.</p>
  
  <p>In the Craft/Twig world, are there similar common pitfalls to avoid when thinking about template performance? What kind of things should I watch for?</p>
  
Tags: <performance>
Title: What are the most common template performance gotchas to avoid?
ThreadText: # What are the most common template performance gotchas to avoid?
  
  (Asked by: username_0 on 2014-06-12)
  
  I'm coming to Craft as someone experienced with Expression Engine templates. When I'm coding for EE I try to avoid certain big ticket performance hits, for example:
  
  Using embeds excessively.
  Using the disable parameter.
  Using simple conditionals vs. advanced conditional when possible.
  Keeping a close eye on the query count.
  Etc.
  
  In the Craft/Twig world, are there similar common pitfalls to avoid when thinking about template performance? What kind of things should I watch for?
  
  **Tags:** performance
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_4 on 2014-06-12. Score: 29 | Accepted answer
  
  Avoiding unnecessary DB queries is definitely the main thing to watch out for. Here’s an example that came up recently in support: Someone wanted to display the first 6 Matrix blocks in an entry, outputting them 2 at a time. Initially the template code looked something like this:
  
  ```
  {% for block in entry.matrixField.limit(2) %}
      <!-- Get the 1st two items -->
  {% endfor %}
  
  {% for block in entry.matrixField.offset(2).limit(2) %}
      <!-- Get the next two items -->
  {% endfor %}
  
  {% for block in entry.matrixField.offset(4).limit(2) %}
      <!-- Get whatever's left, not more than 2 -->
  {% endfor %}
  
  ```
  
  Rather than doing that, we suggested that all 6 blocks be grabbed up front, and output 2 at a time using Twig’s [slice filter](http://twig.sensiolabs.org/doc/filters/slice.html):
  
  ```
  {% set blocks = entry.matrixField.limit(6) %}
  
  {% for block in blocks|slice(0, 2) %}
      <!-- Get the 1st two items -->
  {% endfor %}
  
  {% for block in blocks|slice(2, 2) %}
      <!-- Get the next two items -->
  {% endfor %}
  
  {% for block in blocks|slice(4, 2) %}
      <!-- Get whatever's left, not more than 2 -->
  {% endfor %}
  
  ```
  
  Remember that you can always see what DB queries are happening in a given request by enabling [Dev Mode](http://buildwithcraft.com/docs/config-settings#devMode) and checking your browser’s Javascript console, where all DB queries for the current request will be logged, ordered by how long they took. It will also show if the exact same query was run multiple times.
  
  > **Comments:**
  > **username_0**: Regarding query reduction, I also recently discovered eager loading related elements. https://craftcms.com/docs/templating/eager-loading-elements  
  
  ---
  
  ### Answer by username_1 on 2014-06-12. Score: 18
  
  Template performance isn't a huge worry in Craft thanks to the built-in [cache tag](http://buildwithcraft.com/docs/templating/tags#cache). Even if some code is taking time to process, it can be wrapped in that tag to greatly improve performance.
  
  But to more thoroughly answer your question, the scenarios where that tag might have to be used generally revolve around the number of DB queries. Craft's relationship engine it's pretty great, but it can lead to templates making many DB calls. Sometime's they're unavoidable but you can squeeze a bit more performance out.
  
  In particular, the [ElementCriteriaModel](http://buildwithcraft.com/docs/templating/elementcriteriamodel) (used to retrieve entries, assets, users etc) has some helpful, performance-enhancing methods. For example, instead of retrieving all entries for a section and then only selecting the first one, you can use the `.first()` method.
  
  ```
  {# Fetches all entries #}
  {% set firstEntry = craft.entries.section('yourSection').find()[0] %}
  
  {# Only fetches first entry #}
  {% set firstEntry = craft.entries.section('yourSection').first() %}
  
  ```
  
  > **Comments:**
  > **username_2**: Just be careful that you understand the `{% cache %}` tag correctly. By default it generates a unique cache item for each URL that uses the block, and for large sites this can actually slow down your site over time. See https://craftcms.stackexchange.com/a/22642/5437  
  > **username_3**: These days, `first()` is deprecated. Use `one()` instead.  
  
  ---
  
  