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:

  1. minify your real-life CSS with YUI compressor (online version here) and then
  2. 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

10 Responses to “Sorting CSS to reduce gzip file sizes?”

  1. Twitter Trackbacks for Sorting CSS to reduce gzip file sizes? / phpied.com [phpied.com] on Topsy.com Says:

    [...] 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 [...]

  2. Sorin Stefan Says:

    Thanks Stoyan, I was always curios to see if it’s indeed worth it.
    Have a good one,

  3. Boštjan Says:

    I crunched 52KB big css file and result was 3,7% smaller than original gzipped file. That’s not so bad actualy…

  4. Stoyan Says:

    Thanks Boštjan, was the original 52K gzipped?

    Forgot to mention that my tests were with 10K+ and 10L- _gzipped_ files.

  5. Boštjan Says:

    I tested http://www.vijavaja.com/css/layout.css which I first minified..

  6. 4 Reasons That You Should Start Using Css Today | Web Design and all stuff related to it! Says:

    [...] Sorting CSS to reduce gzip file sizes? / phpied.com [...]

  7. 網站製作學習誌 » [Web] 連結分享 Says:

    [...] Sorting CSS to reduce gzip file sizes? [...]

  8. Text Highlight PHP:Text_Highlighter PEAR & CSS file | Web Design and all stuff related to it! Says:

    [...] Sorting CSS to reduce gzip file sizes? / phpied.com [...]

  9. Why CSS is good for Google? | Web Design and all stuff related to it! Says:

    [...] Sorting CSS to reduce gzip file sizes? / phpied.com [...]

  10. design Says:

    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

Leave a Reply