Sorting CSS to reduce gzip file sizes?
Yesterday I came across this post (via Ajaxian), it talks about how you might be able to end up with smaller file sizes (after gzipping) if you help gzip a little bit by sorting the CSS properties and values in stylesheets, so they are as similar and close to each other as possible.
Now obviously it's not easy to do a safe sorting, there's many edge cases to be taken care of, but the question before diving into safeness is - is it really worth it?
In attempt to answer that I did a very simple CSS parser in JavaScript that will sort the properties and values, then I tested with a few files.
Long story short - it's probably not worth it. But if you want to help, please use the online tool and report if you see a considerable benefit for your own stylesheets.
How it works
Again, bear in mind this is not safe to do, your pages might look different before and after optimization. This is just a test if alphabetizing is really worth it in terms of gzipped filesize.
Before:
b {
b: b c a;
a: a b c;
}
a {
a: a;
}
After
b {
a: a b c;
b: a b c;
}
a {
a: a;
}
As you can see, order of selectors is preserved, properties within each CSS block are sorted and their values are sorted too.
Help by testing your CSS
Since the original question was - is it worth modifying YUI Compressor to sort properties and values, you can help by following these two steps:
- minify your real-life CSS with YUI compressor (online version here) and then
- paste the result in my parser/sorter tool to see if you can gain much after compression.
Please share your results in the comments section of this post.
Results so far?
I tested with two files well under 10K and the results were ever-so-slightly negative - the results were 0.05% bigger.
Tested also with a few files over 10K and the results were 1.5% savings. Hardly worth it. But I hope someone can prove me wrong and show cases where alphabetizing can be well worth it. After all, the sweetest optimization is the one you don't have to do, but your build process can manage by itself
This entry was posted on Wednesday, September 2nd, 2009 and is filed under CSS, performance, yui. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

September 2nd, 2009 at 10:21 pm
[...] Sorting CSS to reduce gzip file sizes? / phpied.com http://www.phpied.com/sort-css-gzip-file-size – view page – cached Stoyan’s blog about (x)html, ajax, bookmarklets, browsers, css, firebug, javascript, json, mdb2, mysql, pear, performance, php, phpbb, tools, yslow, yui, writing, music,… life and everything. — From the page [...]
September 3rd, 2009 at 9:00 am
Thanks Stoyan, I was always curios to see if it’s indeed worth it.
Have a good one,
September 4th, 2009 at 5:29 am
I crunched 52KB big css file and result was 3,7% smaller than original gzipped file. That’s not so bad actualy…
September 4th, 2009 at 10:56 am
Thanks Boštjan, was the original 52K gzipped?
Forgot to mention that my tests were with 10K+ and 10L- _gzipped_ files.
September 4th, 2009 at 12:54 pm
I tested http://www.vijavaja.com/css/layout.css which I first minified..
September 13th, 2009 at 3:57 pm
[...] Sorting CSS to reduce gzip file sizes? / phpied.com [...]
September 21st, 2009 at 11:09 pm
[...] Sorting CSS to reduce gzip file sizes? [...]
September 27th, 2009 at 12:42 pm
[...] Sorting CSS to reduce gzip file sizes? / phpied.com [...]
September 27th, 2009 at 12:44 pm
[...] Sorting CSS to reduce gzip file sizes? / phpied.com [...]
December 15th, 2012 at 12:18 am
Whats up very cool site!! Man .. Excellent .. Amazing .
. I’ll bookmark your website and take the feeds also? I am glad to find so many helpful information here in the publish, we need develop extra strategies in this regard, thank you for sharing. . . . . .
My site :: design