ColdFusion and Whitespace – Part 1

I know quite a few CF developers who are happy if their code runs, then there a the developers who watch the time that it takes for their code to run, down to the millisecond to make sure that their site is as fast as it can be but there is more to a web page than the time it takes to execute. Of course, this is important but there are other things that can speed up or slow down your page beyond your CF code.

The most often forgotten is whitespace. It is all the extra space that is in between your HTML code, typically it is just tabs, spaces and new line characters but these all add up when it comes to the size of the page that you return to the browser. If your page is 100k and 10% of it is whitespace. If you remove this extra 10k, you can get an easy performance jump from your server – it will be able to output 10% more html without using any more bandwidth. Simple math really.

Over the next few days, I want to dig into a few different things you can do to keep it down.
The first and most easiest thing to do is to turn on the ‘Enable Whitespace Management’ feature inside the ColdFusion Administrator (it is on the settings page) This is basically a must have for any production environment. Like every setting you do need to consider how much of a performance impact this will have on your app but I have never found this to be enough of a load to warrant it being turned off.
In your development environment however, this is something you will want to keep turned off. You will want to see just out much whitespace your code is pumping out without it so you can go to work on it at the source.
The next thing to add to your arsenal is <cfsetting enablecfoutputonly=”yes”>. This tells ColdFusion to only return to the browser things inside tags. There is one drawback to this technique is that it cascade down through a request. Why does it do this? Well it is a setting, so it is turned on and off at the request level. If you turn it on (or off) in an include or a custom tag, it will affect the whole request.
To make things a little more confusing, it is also accumulative, if you turn it on twice in one request, then you will need to turn it off twice in the same request before ColdFusion will behave like normal. So the best thing to do is make sure you turn enableCFOutputOnly at the end of any file that you turn on in.

On the ever present performance question, yes this does impact performance, again not by much but you do need to understand what is happening if you wrap every last bit of HTML code in an output tag to ensure that it makes it to the page. The tag tells CF to look closely at this and try to execute it. Now this is only affects the first load of a page, when CF complies it to java byte code, so if you notice it, you are doing better than me!

Tomorrow, in the next post, we will tackle cfsilent and cfprocessingdirective…