Optimized stylesheets

05 March 2004   1 comment   This site

https://www.peterbe.com/stylesheet.css

Mind That Age!

This blog post is 13 years old! Most likely, its content is outdated. Especially if it's technical.

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
Follow @peterbe on Twitter

Comments

Peter
Thank you for posting a comment

Your email will never ever be published


Related posts

Previous:
World Press Photo, the winners 04 March 2004
Next:
Moving Image Contest for Creative Commons 05 March 2004
Related by Keyword:
Ultrafast loading of CSS 01 September 2017
Fastest way to match a filename's extension in Python 31 August 2017
Don't forget your sets in Python! 10 March 2017
Optimization of QuerySet.get() with or without select_related 03 November 2016
CSS Bloat Comparison 03 June 2016
Related by Text:
The problem with CSS 21 December 2004
Optimize Plone.org with slimmer.py 15 February 2005
How to no-mincss links with django-pipeline 03 February 2016
django-static version 1.5 automatically taking care of imported CSS 11 January 2011
CSS Bloat Comparison 03 June 2016