Optimized stylesheets

05 March 2004   1 comment   This site


Powered by Fusion×

I have been experimenting recently with HTML optimization but haven't applied it yet. But I have applied this now to my stylsheets. The size gain is 33%! (1577 bytes to 1027 bytes) However, the speed gain involves also the time to perform the optimization so the speed gain will obviously be less than 33%. But the optimization takes, on this slow computer, 0.004 seconds so in approximate terms the speed gain is also 33%. This is on a stylesheet file with some but short and few comments.

The optimization script removes almost all unnecessary whitespace (newline characters included) and all comments. The code for python friends looks like this:

import re
css_comments = re.compile(r'/\*.*?\*/', re.MULTILINE|re.DOTALL)
def _css_slimmer(css):
   css = css_comments.sub('', css)
   css = re.sub(r'\s\s+', '', css)
   css = re.sub(r'\s+{','{', css)
   css = re.sub(r'\s}','}', css)
   css = re.sub(r'}','}\n', css)
   return css


Your email will never ever be published

Related posts

World Press Photo, the winners 04 March 2004
Moving Image Contest for Creative Commons 05 March 2004
Related by keywords:
Fastest way to uniqify a list in Python 14 August 2006
mincss "Clears the junk out of your CSS" 21 January 2013
Gzip rules the world of optimization, often 09 August 2014
premailer.py - Transform CSS into line style attributes with lxml.html 11 July 2009
The awesomest way possible to serve your static stuff in Django with Nginx 24 March 2010
Optimization of getting random rows out of a PostgreSQL in Django 23 February 2011
A Django base class for all your Forms 16 November 2013
Fastest way to thousands-commafy large numbers in Python/PyPy 13 October 2012
HTML whitespace "compression" - don't bother! 11 March 2013
premailer now excludes pseudo selectors by default 27 May 2013
mincss in action - sample report from the wild 22 January 2013
Optimizing MozTrap 04 June 2014