title: JSF Performance improvement
body: <p>I'm working on a WebApp using JSF 2.2 + Primefaces... project is growing fast, and first performance test was very poor (due to my poor knowledge on JSF lifecycle in combination to non-functional requirements - i.e. fully ajax website), then we could improve a little but results still, are not as expected. (We are having a time of 300~1500 ms depending on the action, idea is to have a performance around 500ms, give or take). Mainly the <strong>Restore View</strong> phase and the <strong>Render Response</strong> are the time consumers (on other phases, time spent is worthless). For some actions, <strong>Invoke Application</strong> also takes time (due to DB calls).</p>
  
  <p>After reading lots of articles on the web, there were a lot of awesome tips to be taken into account (many of course from StackOverflow) such as:</p>
  
  <blockquote>
    <ul>
    <li>Improving DB queries</li>
    </ul>
  </blockquote>
  
  <p>we have some complex ones which are done with two Hibernate Criteria queries so we have to work here. (maybe use pure SQL queries, and on complex ones use sub-querying?)</p>
  
  <blockquote>
    <ul>
    <li>Never defining business logic on Bean's getters</li>
    </ul>
  </blockquote>
  
  <p>Got It!</p>
  
  <blockquote>
    <ul>
    <li>Setting the appropriate scopes to bean and storing on them nothing more than the necessary stuff</li>
    </ul>
  </blockquote>
  
  <p>We have a full ajax site, so View Scoped are almost as Session Scoped to us, we are using the SessionBeans as a sort of 'Cache' to store key data which we don't want to get from DB every time + define businesses logic here.</p>
  
  <blockquote>
    <ul>
    <li>Choosing the right State saving method of JSF - Client Vs Server</li>
    </ul>
  </blockquote>
  
  <p>For this, I have to research some more, check on the possibilities with their pros and cons, then test performance on each one.</p>
  
  <p>So far very clear, now some extra tips on which I have some doubts.</p>
  
  <blockquote>
    <ul>
    <li>Use vanilla HTML as much as possible, and preferably use h: tags rather than p: tags</li>
    </ul>
  </blockquote>
  
  <p>Plain HTML is clear and make sense, now between h: and p: how much is it worthy? For example.</p>
  
  <pre><code>&lt;p:commandButton value="Value" 
               styleClass="class" 
               actionListener="#{myBean.doStuff()}"
               ajax="true" process="@form" update="@form" 
               onsuccess="jsFunction()" /&gt;
  </code></pre>
  
  <p>vs</p>
  
  <pre><code>&lt;h:commandButton value="Value" 
               styleClass="class" 
               actionListener="#{myBean.doStuff()}" &gt;
         &lt;f:ajax execute="@form" render="@form" event="onclick" /&gt;
  &lt;/h:commandButton&gt;
  </code></pre>
  
  <p>or</p>
  
  <pre><code>&lt;ui:fragment... vs &lt;p:fragment...
  </code></pre>
  
  <p>or</p>
  
  <pre><code>&lt;p:outputLabel value="#{myBean.value}" rendered="#{myBean.shouldRender}" /&gt;
  </code></pre>
  
  <p>vs</p>
  
  <pre><code>&lt;ui:fragment rendered="#{myBean.shouldRender}"&gt;
      &lt;label&gt;#{myBean.value}&lt;/label&gt;
  &lt;/ui:fragment&gt;
  </code></pre>
  
  <p>I've been using a mix of Primefaces with Jsf tags and some plain HTML here and there for a while now.(mainly PF due to their component's features) I now that plain HTML will always be faster, but between JSF and another Framework? If I go for this, changing it will take plenty of time and I wouldn't like the outcome of knowing it doesn't make a relevant difference at all.</p>
  
  <blockquote>
    <ul>
    <li>Custom Facelets tags vs Composite Components</li>
    </ul>
  </blockquote>
  
  <p>I think here's the key. Still have some doubts about their differences, on the implementation of both, <em>CC</em> are pretty simple and flexible to use but have the disadvantage that they are fully included on the ViewTree and JSF re-generates for each request (if I'm not mistaken), while <em>custom tags</em> seems a little bit more complex to use (not that much) but has the advantage that only what is actually rendered is included on the ViewTree and nothing more, making the RESTORE VIEW less time consuming. We have several composite components and none Facelets Tags, so here it will be much of the work to do. I still haven't found a good article explaining differences on them, when one should be used and when the other one (have read that for inputs, messages use TAGS and for more complex things CC). If the idea is to prefer tags vs CC, which would be the case on which I would have no option rather than using CC? Is it ok to use custom tags inside CC to make them lighter to JSF for processing them?</p>
  
  <p>I'm about to get into a journey of modifying hole project in order to get a better performance which will take me a couple of days, idea is to get better results this time; so every tip, advice and suggestion is very welcomed! Thanks for your time guys!</p>