2

For a lot of my site I'm using get_template_part mostly because I had coded most of it in html/css first and it just seemed easier to call each part as I saw fit.

My question is, basically, will this have a noticeable impact on performance? If I'm calling in say 3 or 4 template parts. How many would you say is too many, if I was only bringing in little snippets.

Help appreciated.

10

In my opinion three or four calls of get_template_part() is perfectly acceptable and even within the norm for some of the larger and more complex themes out there.

I don't know how many is too many but I think it would have to be a pretty large number to notice a hit in performance.

A Deeper Look

get_template_part() calls locate_template().

locate_template() uses a foreach() loop to run file_exists() against your theme directory.

Performance of file_exists()

According to this post about the performance of file_exists(), PHP automatically caches the results of the function to improve performance. Overall it is considered to be rather fast.

Performance of foreach()

This post about the performance of foreach() vs for() concludes that generally there is little discernible difference between the two. However, it doesn't directly address the performance of foreach() on its own. For that, this post on how foreach() really works dives deep into the function. It states the following.

1.foreach() can be slow because it has to copy the array but in most cases usually isn't.

2.foreach() is usually transparent in its behavior.

3.foreach() can behave strangely if the array is modified within the loop.

In summary

It would appear that a call to get_template_part() should be relatively quick. A loop within locate_template() must be used and foreach() appears to be as good a tool for the job as any other. file_exists() will then cache its results. The only direct control we have over the performance of get_template_part() is the number of files within the template directory.

Unless you have hundreds of template files to look through or are making an equally silly number of calls to get_template_part() the change in your site's performance should be negligible.

  • Great investigative and detailed Answer, kudos for that. – brasofilo Aug 20 '13 at 23:49
2

3 or 4 get_template_parts calls are not going to make any noticeable impact on performance whatsoever. You'd probably have to get into the hundreds before perceiving any issue (if then).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.