resources.
Contents
Data compression 101-777777777
Once we’ve eliminated any unnecessary resources, the next step is to minimize the total size of the remaining resources the browser has to download - i.e. compress them. Depending on the resource type - text, images, fonts, and so on - we have a number of different techniques at our disposal: generic tools that can be enabled on the server, pre-processing optimizations for specific content-types, and resource specific optimizations that require input from the developer.
Delivering the best performance requires the combination of all of these techniques.
TL;DR
- Compression is the process of encoding information using fewer bits
- Eliminating unnecessary data always yields the best results
- There are many different compression techniques and algorithms
- You will need a variety of techniques to achieve the best compression
To illustrate the core principles of these techniques in action, let’s consider how we can go about optimizing a simple text message format that we’ll invent just for this example:
# Below is a secret message, which consists of a set of headers in
# key-value format followed by a newline and the encrypted message.
format: secret-cipher
date: 04/04/14
AAAZZBBBBEEEMMM EEETTTAAA
- Messages may contain arbitrary annotations, which are indicated by the “#” prefix. Annotations do not affect the meaning or any other behavior of the message.
- Messages may contain “headers” which are key-value pairs (separated by “:”) and have to appear at the beginning at the message.
- Messages carry text payloads.
- Well, the comment is interesting, but we know that it doesn’t actually affect the meaning of the message, so we eliminate it when we’re transmitting the message.
- There are probably some clever techniques we could use to encode headers in an efficient manner – e.g. we don’t know if all messages always have “format” and “date”, but if they did, we could convert those to short integer IDs and just send those! That said, we’re not sure if that’s the case, so we’ll leave it alone for now.
- The
payload is text only, and while we don’t know what the contents of it
really are (apparently, it’s using a “secret-message”), just looking at
the text seems to show that there is a lot of redundancy in it. Perhaps,
instead of sending repeated letters, we can just count the number of
repeated letters and encode them more efficiently?
- E.g. “AAA” becomes “3A” - or, sequence of three A’s.
format: secret-cipher
date: 04/04/14
3A2Z4B3E3M 3E3T3A
The new message is 56 characters long, which means we
managed to compress our original message by an impressive 72% - not bad,
all things considered, and we’re only getting started!Of course, you may be wondering, this is all great, but how does this help us optimize our web pages? Surely we’re not going to try to invent our compression algorithms, are we? The answer is no, we won’t, but as you will see, we will use the exact same techniques and way of thinking when optimizing various resources on our pages: preprocessing, context-specific optimizations, and different algorithms for different content.
Minification: preprocessing & context-specific optimizations
TL;DR--------999999999999999999999999
- Content-specific optimizations can significantly reduce the size of delivered resources.
- Content-specific optimizations are best applied as part of your build/release cycle.
<html>
<head>
<style>
/* awesome-container is only used on the landing page */
.awesome-container { font-size: 120% }
.awesome-container { width: 50% }
</style>
</head>
<body>
<!-- awesome container content: START -->
<div>…</div>
<!-- awesome container content: END -->
<script>
awesomeAnalytics(); // beacon conversion metrics
</script>
</body>
</html>
- Code comments are a developer’s best friend, but the browser does not need to see them! Simply stripping the CSS (
/* … */
), HTML (<!-- … -->
), and JavaScript (// …
) comments can significantly reduce the total size of the page. - A “smart” CSS compressor could notice that we’re using an inefficient way of defining rules for ‘.awesome-container’ and collapse the two declarations into one without affecting any other styles, saving yet more bytes.
- Whitespace (spaces and tabs) is a developer convenience in HTML, CSS, and JavaScript. An additional compressor could strip out all the tabs and spaces.
<html><head><style>.awesome-container{font-size:120%;width: 50%}
</style></head><body><div>…</div><script>awesomeAnalytics();
</script></body></html>
Taking a step back, the above example illustrates an important point: a general purpose compressor - say one designed to compress arbitrary text - could probably also do a pretty good job of compressing the page above, but it would never know to strip the comments, collapse the CSS rules, or dozens of other content-specific optimizations. This is why preprocessing / minification / context-aware optimization can be such a powerful tool.
Remember
- Case in point, the uncompressed development version of the JQuery library is now approaching ~300KB. The same library, but minified (removed comments, etc.) is about 3x smaller: ~100KB.
In short, as a first step in optimizing the efficiency of your assets, build an inventory of the different content types and consider what kinds of content-specific optimizations you can apply to reduce their size - doing so can yield significant savings! Then, once you’ve figured out what they are, automate these optimizations by adding them to your build and release processes - that’s the only way you can guarantee that the optimizations will stay in place.
Text compression with GZIP
TL;DR
- GZIP performs best on text-based assets: CSS, JavaScript, HTML
- All modern browsers support GZIP compression and will automatically request it
- Your server needs to be configured to enable GZIP compression
- Some CDNs require special care to ensure that GZIP is enabled
All modern browsers support and automatically negotiate GZIP compression for all HTTP requests: our job is to ensure that the server is properly configured to serve the compressed resource when requested by the client.
Library | Size | Compressed size | Compression ratio |
---|---|---|---|
jquery-1.11.0.js | 276 KB | 82 KB | 70% |
jquery-1.11.0.min.js | 94 KB | 33 KB | 65% |
angular-1.2.15.js | 729 KB | 182 KB | 75% |
angular-1.2.15.min.js | 101 KB | 37 KB | 63% |
bootstrap-3.1.1.css | 118 KB | 18 KB | 85% |
bootstrap-3.1.1.min.css | 98 KB | 17 KB | 83% |
foundation-5.css | 186 KB | 22 KB | 88% |
foundation-5.min.css | 146 KB | 18 KB | 88% |
- Apply content-specific optimizations first: CSS, JS, and HTML minifiers.
- Apply GZIP to compress the minified output.
What’s the best config for your server? The HTML5 Boilerplate project contains sample configuration files for all the most popular servers with detailed comments for each configuration flag and setting: find your favorite server in the list, look for the GZIP section, and confirm that your server is configured with recommended settings.

A quick and simple way to see GZIP in action is to open Chrome DevTools and inspect the “Size / Content” column in the Network panel: “Size” indicates the transfer size of the asset, and “Content” the uncompressed size of the asset. For the HTML asset in above example, GZIP saved 98.8 KB during transfer!
Remember
- Believe it or not, there are cases where GZIP can increase the size of the asset. Typically, this happens when the asset is very small and the overhead of the GZIP dictionary is higher than the compression savings, or if the resource is already well compressed. Some servers allow you to specify a “minimum filesize threshold” to avoid this problem.
Nhận xét
Đăng nhận xét