Update: The count.js path referenced in this article now 404s. I'm leaving this post here for reference; but recommend you do some investigation before just copy/pasting code!

With search engines using page loads times as a factor in ranking results, making sure your website loads quickly is becoming more and more important. Gone are the days when we used to say "If your website doesn't load in 10 seconds, people will go elsewhere". Nowadays, people expect websites to load in tenths of seconds, not seconds.

There are a huge number of browser addons/tools that will help you measure the performance of your website, but my current favourite way to check is using loads.in. Some of the things highlighted by loads.in when I entered the address of this blog were trivial to fix (loading Google's API and jQuery when neither are being used, for example!). Some others required a little more thought :(

The next-slowest target was the Disqus comments system. It wasn't particularly slow, but there was one part that frustrated me:

The request for http://dantup.disqus.com/count.js takes around 600ms and just returns a 302 redirect to http://mediacdn.disqus.com/1301599987/build/system/count.js.

I'm not entirely sure of the reason for this redirect, but it's adding 600ms to my page being ready. This extra round-trip will also be far slower on a mobile device, and with 3G tablets become more popular, it makes sense to try and eliminate this.

This looks pretty easy to "fix", but before changing the path to be hard-coded to the mediacdn address, I asked Disqus if they saw any problems with this (after all, the redirect might be because the mediacdn address may change). Unfortunately, they weren't really sure if this would be a problem :(

Another possible problem is that the redirect could be different depending on the user agent. If I hard-code it, this would be gone. After a little testing, I decided this wasn't the case, and the redirect was always the same (and this makes sense, because the work is still exactly the same regardless of the user agent - not to mention browser-sniffing this way being a terrible idea).

I decided to give it a try anyway. It's just comment counts. If the URL changes, the comment counts will be broken until I notice. That's hardly the end of the world. So, my change looks like this:

// Old Code
<script type="text/javascript">
	var disqus_shortname = 'dantup';

	(function () {
		var s = document.createElement('script'); s.async = true;
		s.type = 'text/javascript';
		s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
		(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
	} ());
</script>

// New Code
<script type="text/javascript">
	var disqus_shortname = 'dantup';

	(function () {
		var s = document.createElement('script'); s.async = true;
		s.type = 'text/javascript';
		s.src = 'http://mediacdn.disqus.com/13015111111199987/build/system/count.js';
		(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
	} ());
</script>

I tested this using loads.in, and it actually took two full seconds off the time until the onload event fired, from around 4s to 2s! As well as eliminating the extra round trip which was pretty slow, there's no longer a DNS lookup for dantup.disqus.com.

So, assuming Disqus don't change this, it looks like a fairly safe way to speed up a site with Disqus comment counts. If you're going to try it yourself, be sure you work out the correct count.js path by following your own redirect! I edited the numbers in the URL of my sample code to avoid people copy/pasting my URL in.

I'd love to see Disqus eliminate this roundtrip themselves, so everybody using Disqus would get this little boost. Until then, this seems to be a decent workaround.