-
Website
http://www.davidcramer.net/ -
Original page
http://www.davidcramer.net/code/369/spaceless-html-in-django.html -
Subscribe
All Comments -
Community
-
Top Commenters
-
Rob Hudson
1 comment · 3 points
-
creeva
3 comments · 1 points
-
David Cramer
93 comments · 1 points
-
MsUnderestimated
1 comment · 14 points
-
Matt B.
1 comment · 2 points
-
-
Popular Threads
taking a file:
index.html - 91565 bytes
code:
>>> f = open('index.short', 'w')
>>> f.write( short( file( 'index.html' ).read() ).encode('utf-8') )
>>> f.close()
produces:
index.short - 69915
and my favorite - gzip:
index.gz - 14709
index.short.gz - 13319
examples from zena.centrum.cz where we have some nasty whitespace in our HTML...
Knowing this, I would take mod_deflate in (insert your favorite web server) over any python space stripping anytime...
I would suggest, though (as I see others have), that people facing the same problem have a look at simply gzipping their text-based output at the webserver level. Since the action's probably happening on the webhead either way, it's unlikely to be much more costly in CPU terms, and it's even simpler to implement than your solution (it typically just takes a line or two in .htaccess if you're using Apache). Gzipping will certainly remove any overhead that whitespace represents.
see: http://code.djangoproject.com/browser/django/tr...
http://www.djangosnippets.org/snippets/1055/
to (very) efficiently whitespace optimize inline CSS which might be interesting for you and your Jinja too.
I'm curious: why outputting whitespace-less HTML when you could just use gzip compression on the server for HTML, XML, CSS and JavaScript, and get better results (like from 300 kB -- not kb ;) -- to, say, 120 kB)? Did you do some performance testing regarding that?
Unless gzipping your text output has a clear performance cost, I would keep my HTML code with whitespace (if not perfect, then at least decently readable code). Makes it easier to debug when you need to check the real thing (Firebug only shows what Firefox understands, not what it gets from the server).
Do you happen to use this method, then gzip (through mod_deflate for instance)? If so, how much do you save compared to a gzipped version with all whitespace intact?