<p>If you know which years should appear, and there are no breaks in the timeline, perhaps it would make more sense to do a simple numeric loop instead...</p>

<pre><code>{% for year in (now.year)..2009 %}
    {{ year }}
{% endfor %}
</code></pre>

<p>That will count backwards from the current year to 2009.</p>

CC BY-SA 3.0
2016-07-04T05:58:07.727Z
Or you could use the oldest entry's postDate with `{% set oldestEntry = craft.entries.section('posts').order( 'postDate asc' ).first() %}` then use `oldestEntry.postDate|date( 'Y' )` is place of 2009.
Luke Pearce
CC BY-SA 3.0
2016-07-04T08:47:54.303Z
NICE idea @Luke Pearce!
Matt P
CC BY-SA 3.0
2016-07-04T14:53:21.453Z
Great point @LukePearce! You could do the same to get the newest entry (in case it's not from the current year). So at most, you'd only be querying for 2 entries.
Lindsey D
CC BY-SA 3.0
2016-07-04T16:30:38.500Z
Perfect, thank you Lindsey and @LukePearce !
Bryant
CC BY-SA 3.0
2016-07-04T03:27:57.373Z
2016-07-04T03:27:57.373Z
Lindsey D
<p>I seem to be having trouble with the performance of my site (on localhost) after including this object. Im using it to build a menu for an archive of the posts section which has about 2,500 entries inside of it. </p>

<pre><code>{% set allEntries = craft.entries.section('posts').limit(null) %}

{% for year, entriesInYear in allEntries | group("postDate.year") %}
    {{ year }}
{% endfor %}
</code></pre>

<p>This fetch request spits out the following:</p>

<blockquote>
  <p>2016 2015 2014 2013 2012 2011 2010 2009</p>
</blockquote>

<p>When I make this fetch request, my page is reload time is ~11s. If I comment out this request my page reload time is ~3s. </p>

<p>Is there a more economical way to return the years associated with my posts?</p>

<templating><elementcriteriamodel><performance><limit>
A more economical method of building an archive menu?
# A more economical method of building an archive menu?

(Asked by: username_0 on 2016-07-03)

I seem to be having trouble with the performance of my site (on localhost) after including this object. Im using it to build a menu for an archive of the posts section which has about 2,500 entries inside of it. 
{% set allEntries = craft.entries.section('posts').limit(null) %}

{% for year, entriesInYear in allEntries | group("postDate.year") %}
    {{ year }}
{% endfor %}

This fetch request spits out the following:

2016 2015 2014 2013 2012 2011 2010 2009

When I make this fetch request, my page is reload time is ~11s. If I comment out this request my page reload time is ~3s. 
Is there a more economical way to return the years associated with my posts?

**Tags:** templating, elementcriteriamodel, performance, limit

---

## 1 answers

### Answer by username_1 on 2016-07-04. Score: 5 | Accepted answer

If you know which years should appear, and there are no breaks in the timeline, perhaps it would make more sense to do a simple numeric loop instead...
{% for year in (now.year)..2009 %}
    {{ year }}
{% endfor %}

That will count backwards from the current year to 2009.

> **Comments:**
> **username_2**: Or you could use the oldest entry's postDate with `{% set oldestEntry = craft.entries.section('posts').order( 'postDate asc' ).first() %}` then use `oldestEntry.postDate|date( 'Y' )` is place of 2009.  
> **username_3**: NICE idea @Luke Pearce!  
> **username_1**: Great point @LukePearce! You could do the same to get the newest entry (in case it's not from the current year). So at most, you'd only be querying for 2 entries.  
> **username_0**: Perfect, thank you Lindsey and @LukePearce !  

---

