Weblog posts tagged with html
Impact of attribute order on compression
One of my personal coding standards is to keep similar HTML attributes grouped together in a consistent order at the start of the element, e.g.
1 2 |
<script type="text/javascript" src="foo"></script> <script type="text/javascript" src="bar"></script> |
vs.
1 2 |
<script src="foo" type="text/javascript"></script> <script src="bar" type="text/javascript"></script> |
This helps with readability, maintainability, &c. &c.
Recently another thought occurred to me: that this would also help with HTTP compression.
I ran a quick test using these two (very simplified) HTML documents:
With the following results (taken from the Firebug Net panel):
| File | Size (uncompressed) |
Size (compressed) |
|---|---|---|
| ordered.html | 240b | 145b |
| unordered.html | 240b | 148b |
So, the ordered version gives us a saving of 3 bytes – not a massive gain, but in a much larger document it could be significant.
I imagine this would also hold true (and possibly give much more significant savings) for CSS if property/value pairs that are the same are grouped together.
For those of you interested in such things, I tested the compressed vs. uncompressed HTML by first soft linking the HTML files:
ln -s ordered.html ordered-uncompressed.html
ln -s unordered.html unordered-uncompressed.html
The adding the following .htaccess:
AddOutputFilterByType DEFLATE text/html
SetEnvIfNoCase Request_URI uncompressed no-gzip dont-vary
which disables output compression for any URI that contains ‘uncompressed’.