Suppressing White Space

…posted by davidjmedlock:

Over the weekend I happened to be working on a project with another developer and he emailed me about an issue with a custom tag that he was using. He said that there was an inordinate amount of white space in his documents. In fact, the document with all the white space in it was about 40KB as opposed to the document without white space being around 16KB. Yikes!

The reason for this is because in the execution of the page, the server must translate the CFML into p-code, which is executable by the application server. In the conversion, the CFML becomes whitespace.

When you have ColdFusion code in a display document and then view the source, you’ll see that everywhere the ColdFusion code was is now white space. This can cause your documents to be bloated and it can eat up your bandwidth quickly.

The best way that I’ve discovered for suppressing whitespace is to go into ColdFusion Administrator, go to the Settings page and click “Enable Whitespace Suppression”. This will drastically reduce the size of your pages, especially if you are doing anything database intensive where you have large queries. It will also make your code more readable.

I know that there are ways to do this in code, using cfprocessingdirective, cfsetting, and cfsilent, but with the ability to set it up in the server itself, I really don’t see any reason to have to do it in code. Even if you’re on a shared host, you should email them and ask them to make this small change for you. It will decrease the bandwidth used and they should be able to do it for you without a problem. (If they ask you what the ColdFusion Administrator is, then find another host!)

There’s more information about displaying output in the Programming ColdFusion book by OReilly. Chapter 11 is where you’ll want to look.

As a side note, I looked around a little bit to find out if enabling the suppression of whitespace has any noticeable effect on server performance and I haven’t seen anything in there about it. I could see how it might slow it down ever so slightly, since it now has to clean up after itself, but it also seems that it may speed up just a very little bit since it is dealing with less data in terms of memory… If anyone has references on the matter, I’d be interested to see them.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • jonese

    I don’t have any real comments but i can tell you from my experiance the supress white spaces isn’t 100%. I’ve got a CFMX 6.1 server right now which will not supress white spaces, and i have another which will. I’ve tried turning this on and off and even reloaded but it doesn’t want to surpress. Searched high and low for a resolution to no avail. If anyone has idea drop me a line ericATjpecservices.com

  • http://www.delyrical.com davidjmedlock

    This is a dumb question, but did you make sure to restart the CF Server after changing the setting? I noticed on our company’s dedicated server that although it’s benn turned on for quite some time, it still doesn’t suppress 100% of the white space. It’s an odd occurence, but it seemed to work on the server that we had to deal with over the weekend.

  • http://www.morgankelsey.com nagrom

    There is a performance hit that comes with making the admin setting, which is why many hosts won’t make the setting.

    As far as the tags that are available go, cfprocessingdirective is the slowest, it’s best avoided. is a little faster, and works well in some situations. Be sure to reset enablecfoutput to “No” at the end, a closing tag won’t work.

    cfsilent is the fastest, according to macromedia (I heard that at a performance seminar at CFNorth last year). Just be warned that NOTHING comes out between cfsilent tags, not even includes.

    My personal preference is cfsilent, I try to separate my logic and display code as much as possible, so I can wrap all the logic in cfsilent tags.

    Just my 2 cents…

  • http://www.delyrical.com davidjmedlock

    Thanks for that information, Morgan. I wasn’t able to find any performance info on enabling that feature on the web. Maybe I just overlooked it somewhere, though. I had a feeling there would be some performance hit, though.

  • http://www.morgankelsey.com nagrom

    Whelp, I checked the box on my server, and the processor definitely seems a little busier. I’m not seeing a huge step up, but I’m running in the 10-15% usage area where I’m usually 5-10%. I’m gonna leave it and keep on eye on the perf monitor this afternoon when the server gets busier.

    I think it has the same effect as wrapping all your code in tags, because it didn’t clean *all* the whitespace (same behavior as the

    Ok, so how about this: If you’re dealing with an unstable server, try disabling the suppress whitespace setting and see if performance improves ;-)

  • http://www.delyrical.com davidjmedlock

    Hm, interesting to see the difference there. I think I’m going to suggest that we try that on our production boxes here at the office and see what effect it has on performance. Our servers get hit constantly by hundreds, if not thousands, of users every minute, so if anyone would notice a performance difference, I’m sure we will.

  • http://www.delyrical.com davidjmedlock

    Actually, now that I think of it, it may not be wise to disable that on our servers. Might try it on the dev server. I know for a fact that allowing whitespace will increase the size of some of these pages by three, four, or five times as much and it’s best not to take a chance on pushing our bandwidth like that…

    I may look at the dev server and see how it reacts, though. That’s a fairly low volume server…

  • http://simon.incutio.com/ Skunk

    Just out of interest, is the suppress whitespace option smart enough NOT to do things to whitespace occuring inside a pre tag? If it is, it must be parsing the actual HTML which could lead to a relatively significant performance hit. I’ve been considering implementing whitespace supression for my own work with mod_python but I’m concerned that it would intefere with pre tags, and I don’t really want to run the HTML through a parser just to kill off some additional spacing in the source code.

  • http://www.delyrical.com davidjmedlock

    It doesn’t look like it affects the HTML at all, just removes the excess whitespace left behind after the CFML is parsed. All the HTML tabs and line breaks seem to be intact. As mentioned above, it is not 100% suppression, either. It does tend to leave quite a bit of space.

    White space suppression seems like it would be a good solution for a low traffic site with high page views that could eat up bandwidth fairly quickly. For example, if you have an administrative section of a site where you do a lot of “work” in your ColdFusion code like running multiple queries, lots of logic, calling custom tags, then you may consider flipping the switch to save on bandwidth as long as it won’t affect your visitors adversely.

  • http://www.morgankelsey.com nagrom

    Good blog Dave, there’s definitely situations where the savings are well worth the small performance hit. And, well, I have to say if checking that box crashes your server you have larger problems than bandwidth ;-)

    I run an IIS compressor on my server, it cost about $800. I got the dough back after 3 months of bandwidth savings. But of course that too has a performance hit. Oh – and Skunk – the IIS filters DO compress HTML as well. The super fancy-schmancy ones even recompress images and other binary files. (The free compressor that comes with IIS 5 is junk, don’t use it.)

    I found this too:
    http://www.markme.com/cantrell/archives/002350.cfm
    for the true whitespace fanatic.

  • robi

    nagrom,

    You can do compresion in ColdFusion MX 6.1 on IIS for free with ColdFusion. You just wasted 800 bucks :-)

    http://www.sys-con.com/coldfusion/article.cfm?id=573

    You can do alot of really cool stuff with filters in CF

  • Rob

    In code where I’ve needed to keep things tight I’ve used this http://www.cflib.org/udf.cfm?ID=812 UDF which I found very good. It has three levels of whitespace removal.

  • Ken

    We’ve just recently switched white space suppression on and our servers haven’t taken too much of a performance hit… yet, and we get over 100,000 hits a day. However, I did notice – picking a random page – that before white space suppression file size was 350K and is now 60K.

    It was definitely worth it for us!