How I Increased My WordPress Site’s Speed

|

I spent yesterday optimizing the performance of this site. I love fast page loads and have little patience for sluggish performance. Here’s what I did.

1. Leverage browser caching

Tell your web server to set expiration headers for static resources so browsers know to store them in local disk. This will keep requests off the network altogether. I use Apache:

1
2
3
4
5
6
7
8
9
10
11
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 300 seconds"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
</IfModule>

2. Remove Query Strings From Static Resources

Query strings like ?ver=3.3 can prevent proxies from caching. Some WordPress plugins automatically generate these query strings when calling stylesheets and JavaScript files. I removed them with:

1
2
3
4
5
6
function _remove_script_version($src){
    $parts = explode('?', $src);
    return $parts[0];
}
add_filter('script_loader_src', '_remove_script_version', 15, 1);
add_filter('style_loader_src', '_remove_script_version', 15, 1);

3. Reduce Number of CSS/JS Files

I consolidated my external CSS/JS files because each separate file is a separate request to the server.

4. Use WordPress Super Cache Plugin

This plugin generates static HTML from WordPress’ PHP scripts and serves those instead. I scheduled it to preload pages and refresh them once a day.

5. Use Memcache with WordPress

Reading from memory is orders of magnitude faster than reading from disk. This is a good analogy of the difference from the computer’s perspective.

I have the PECL memcache extension and memcached daemon running on localhost’s port 11211.

I put this in wp-config:

1
2
3
// define memcached_servers for object-cache.php in wp-content/
global $memcached_servers;
$memcached_servers = array('default' => array('127.0.0.1:11211'));

and object-cache.php in wp-content.

6. Test Your Site’s Speed

Pingdom is a good tool. Here’s my site’s performance history.

Resources: