ZF2 Performance QuickTipp #1 – ViewModels

The performance of the default rendering process of Zend Framework 2 has improved when looking back at ZF1. But there are still ways to improve the performance of the rendering process. One of those ways is to always return fully qualified ViewModels. In this article i will clarify what this means and demonstrate how to improve the performance.

The usual approach

Most developers will be familiar with the default approach to render something:

Why this is bad for performance

The PhpRenderer-Strategy of ZF2 will check the return value of a controller-action first. If it is only an array, the framework has to create a blank ViewModel from scratch. After that it will check for a template. If no template is given, the framework has to look up for a template. This is done by checking the current Module, the current Controller and the current Action. Based on those, a Template-File will be matched. After that, the rendering process will start.

How to improve the Performance

So how can we improve this? The answer is as simple as to tell the ViewModel directly which template to render!

And that’s really all there is to it. Of course in higher performance production you’d also include caching and stuff like this, but that’s another subject to be covered at a later point in time. If you want to get the maximum out of your application, always return fully qualified ViewModels so the framework doesn’t have to do the work for you!

Clip to Evernote
2 Responses to ZF2 Performance QuickTipp #1 – ViewModels
  1. Okeowo Aderemi Reply

    hmmm a bit lengthy but if that’s going to save some performance sure why not, nice one please more performance tips and also am still clueless about creating reusable modules.

    • Sam Reply

      More will follow. Reusable modules, just check the ones on http://modules.zendframework.com – check the code, see what they do and you’ll get a grab for it.

Leave a Reply

Your email address will not be published. Please enter your name, email and a comment.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">