I am a little familiar with the idea behind Gzip, thanks to this great video from Frédéric Kayser (in French), however there is a something I am still not sure about.
Basically, a string gets more and more compressed every time it is being repeated. And unless I'm wrong, the longer the string, the better.
Let's talk about Sass for a second. Please consider this mixin:
@mixin center($max-width) {
width: 100%;
max-width: $max-width;
margin: 0 auto;
}
The first line is static. The second line is dynamic since it depends on the $max-width
variable. The third line is static. Let's say we used it a couple of times, getting this CSS:
.abc {
width: 100%;
max-width: 1170px;
margin: 0 auto;
}
.def {
width: 100%;
max-width: 960px;
margin: 0 auto;
}
Now, as far as I know, Gzip will be able to find those repeated patterns:
{
width: 100%;
max-width:
And:
;
margin: 0 auto;
}
Getting back to my question: would it be better to move the dynamic lines on top (or bottom, whatever) of the mixin to group static lines together and benefit from longer strings?
For instance something like:
@mixin center($max-width) {
width: 100%;
margin: 0 auto;
max-width: $max-width;
}
Let's be clear: I am perfectly aware this will make absolutely no difference whatsoever on the file weight. It's just out of curiosity. ;)
This is a good question, but incredibly hard to answer. Gzip is based on DEFLATE and LZ77. Both DEFLATE and LZ77 does something called “duplicate string elimination.” Basically, when a string occurs a second time, it is replaced by a pointer to the first string. This reduces the amount of bytes marginally. But DEFLATE also does “bit reduction” using Huffman coding. That means common symbols with large representations are switched by smaller representations. When decoding, these are switched back to their original representations.
My initial guess is yes, I do think that bundling reoccurring strings actually helps, because then the matched string can be replaced by one pointer, instead of two, however there are many factors at play like file size, the size of matched string, symbol usage and more.
Note: The second repeated pattern would include the semi-colon of previous line and the line break.