windmill_efficiencyYou all must know me by now. I am always nagging you about code efficiency, using minimum plugins and other improvement tactics. The past couple of days there has been some interesting conversation and feedback on Kim’s blog, on the post she made a few months back asking for plugin ideas for me to code. From there the idea for the YAFootnotes and My Tag Cloud plugins have been given plus some valuable feedback. Now, there is another conversation started from a user, Richard. My reply to all his valuable feedback would a) be too long for a comment and b) illustrates what i think might help some other people too. So, i decided to go on with this blog post. Enough with the chit chat, let’s see what he says.

I will take it one at a time. Essentially what he suggests is this:

what I really need is a plugin that selectively loads javascripts and style sheets of other plugins on a page by page basis. WordPress carries around SOOOO much dead weight in scripts that load globally and are used on maybe just 1 or 2 pages on the site.

He carries on with a design suggestion. No what he says is not wrong. WordPress does carry around many scripts that it doesn’t need and many plugins only make it worst. Here is how it works. A plugin needs some sort of Javascript and a style, maybe, to be loaded. So, upon call, it enqueues his scripts and styles for sending back to the client and then it decides whether or not to run. Let’s see a quick example. For instance, a plugin could be searching for a special tag on a page. If it finds it then some special magic happens and replaces it with the actual content. Take an image gallery. You place a special tag on the gallery page, that when the plugin runs identifies it, and then starts creating it’s output (pulling out the albums for the database, creating tables etc). But what happens when the user visits the homepage where there is no gallery what so ever? Well, the plugins style and scripts are loaded anyway. One would say “heck those are 20-50Kb scripts and styles, let it load, whatever!”. But think about having four or five plugins doing the same thing. As Richard rightfully claims:

On one of my sites, I realized I was loading different versions of jquery from different plugins on every page amounting to some 200kb of totally wasted overhead.

Here there was something even worst happening. The plugin authors were loading a jQuery copy of their own, a library that WordPress loads by default! He was absolutely right to be frustrated. So, by now you are totally seeing where he is coming from. But let me tell you, i think we need to take this a little deeper.

First of all, those 200Kb he is claiming to be loaded each time, although wasted most of the time, they are not loaded all the time! Yes, if you make 10 clicks on his site, from his homepage, to an article, to a page, to another article etc those 200Kb will be sent your way only once. And that is because all modern browsers know about caching. Before loading anything, they send to the web server what is called a “HEAD” command. Essentially, what they do is, they check out if the local copy they have has changed, if not, they won’t request it again. I agree that 200Kb are wasted but that’s a small price to pay having all this functionality added to your site (for free if i might add).

One more juicy part is the average blog user. Now, i know many of my readers are passionate bloggers that know little on coding. That is something that happens with most of the bloggers out there. Imagine having a blogger write a page and having to decide which scripts or styles should he load or not. He has absolutely no idea what a script is, let alone decide if he wants to load it or not. I know Richard is talking about a plugin here, which means that it won’t be for the average user, but there was also a talk about expanding the functionality over to WordPress itself. That, in my humble opinion, wouldn’t work.

As a plugin, it could be coded, but, i think wouldn’t save you from much trouble. And here is why. Here is a final point, my major point. In a few words, if you have plugins loading as much as 200Kb of extra scripts, you should really, and i mean really, consider if you need all those. My bet would be on the amount of the plugins rather than poor coding itself. Yes, they are poorly coded but, can you stop using any plugin? The delay won’t be on downloading those extra kilobytes, it will be on extra processing time the script will need to create it’s output. Even if it doesn’t need to do anything at all, just by stopping execution of the platform (in our case WordPress) and calling a plugin function is enough. I know you might think i am exaggerating at this point but, believe me, too many plugins can slow you down sufficiently enough to notice, even if they do absolutely nothing.

All in all, Richard has a strong point, poor coding should be avoided, but, user discretion is required too. Consider very well the fact of installing a plugin before you go on and do it. Just because it’s easy to do it, doesn’t mean it is recommended.

As for a response to my friend Richard, you have a point but coding a plugin and adding more burdain to your blog is not the option in my oppinion.

Photo by Insight Imaging: John A Ryan Photography