View Full Version : Performance: preg_replace or hardcoded?
Maccrob
01-10-2012, 07:20 AM
Hi all,
I have plugins which adds some data to forumbits etc by using $thread[] array and hardcoded html with something like {vb:raw thread.mydata}.
Which approach is the better from the position of performance: to parse template_compile and use e.g. preg_replace, or just hardcoded piece of html into existent template?
Disasterpiece
01-10-2012, 08:56 AM
I recommend using preg_replace only if:
- the amount of text is "not too much" in one request
- the data may change often
- the replacement is context-sensitive, which means a normal str_replace is not sufficient
- the data needs to be fresh (non-cached)
It would be helpful to get a more specific example on what you actually want to do.
Maccrob
01-10-2012, 12:32 PM
Example: add data after
<li id="forum1" class="forumbit_post L2">
on forumhome.
First variant: get needed data $thread[‘mydata’] and use str_replace in template_compile hook to append it after <li
Second variant: add html to forumhome_forumbit_level2_post template.
<li id="forum1" class="forumbit_post L2">{vb:raw thread.mydata}
Of course, for plugin creators, first variant is more suitable due simplest integration with different templates. In second variant we should manually add some code to templates.
So, from the position of performance which variant is better?
Disasterpiece
01-10-2012, 01:57 PM
In any case it's better to use second way with the template system, because templates get cached. So this text gets generated once every few minutes, depending on the cache lifetime and can be viewed by hundrets of users.
The first method would replace a small text into a huge text for every site request, thus increasing the server load unnecessarily.
Also, it's more clean to use the templates as they are intended.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.