The easiest way to speed up your WordPress site is to eliminate all its unwanted resources. The second easiest way to speed it up is to compress all its wanted resources. Enabling GZIP compression on your web server is one of the simplest and most efficient ways to achieve that. All modern browsers include support for GZIP compression by default. However, to serve the compressed resources to your users with no hiccups, you must configure your server properly. In this post, youâll learn the basics of data compression on the web, what GZIP compression is, its various benefits, and how you can use it to speed up your WordPress sites on different server setups. Excited? Letâs decompress! The Basics of Data Compression on the WebData compression on the web is the process of reducing the size of data transmitted by websites. Depending on the data typeâtext, images, stylesheets, scripts, fontsâthere are various ways to go about compressing data. For instance, minification of HTML, CSS, and JavaScript is a simple way to reduce the amount of data sent by browsers. Here, the minifier compresses the text by removing unnecessary characters such as comments and whitespaces from the source code. In the example below of a simple HTML document, there are three content types: HTML markup, CSS styles, and JavaScript code. Each content type has unique syntax and semantics. Overall, this HTML document has 357 characters in total.
The above code is easy to read and follow. Itâs ideal for development. However, itâs unnecessary for the browser to read comments and have perfectly indented tags. A smart compressor can analyze this document and remove all the unnecessary bits from it.
After minification, the HTML document reduces to just 141 characters. Thatâs a 60.50% savings in the number of characters. Itâs not as easy on the eyes, but that doesnât matter to the browser. Itâll still display the same page to the user. To take advantage of both the versions, you can maintain the original document as a âdevelopment version,â but minify all the code before pushing it live. Most WordPress performance plugins take care of this automatically for you. As an example, the uncompressed version of jQuery 3.5.1 library is more than 3 times bigger than the minified version of the same file. Likewise, the same compression techniques described above can optimize other content types like images, videos, fonts, and more. Content-specific reductions like these are the first step in optimizing the size of your websiteâs text-based assets. But thereâs more to data compression than just minification. Using advanced math techniques, data compression algorithms can further reduce the size of data. One of the most popular examples of such a data compression method is GZIP. It enables efficient data transmission and has contributed significantly to make the internet a viable global communications medium. What Is GZIP Compression?GZIP, short for GNU Zip, is the most popular lossless data compression method on the web. It allows you to reduce the size of your siteâs HTML pages, stylesheets, and scripts. Apart from being a data compression algorithm, GZIP is also a file extension (.gz) and a software used for file compression/decompression. Itâs based on the DEFLATE algorithm, which is a mixture of LZ77 encoding and Huffman coding algorithms. A GZIP compressor takes a set of raw data and compresses it losslessly. The raw data could be from any file type, but GZIP works best with text-based assets (e.g. HTML, CSS, JS). The next section covers how GZIP compression works in depth. How GZIP Compresses DataTo begin with, the GZIP compressor runs the LZ77 compression algorithm on the raw data to remove redundancies. This algorithm works by finding repeated patterns in a predefined sliding window (a small section of the raw data). It then replaces all the repeated strings with tuples to compress the raw data. In the above example, the sliding window size is just 13 characters long (13 bytes). However, GZIP compression can use a maximum sliding window size of 32 KB (32,768 bytes). The size of the sliding window plays a key role in LZ77 compression performance. After compressing the raw data with the LZ77 algorithm, the GZIP compressor then uses the Huffman coding algorithm to compress it further. It does this by assigning characters occurring more frequently the least number of bits, while assigning rare characters the highest number of bits. This technique is like the one used in Morse code, wherein letters occurring more frequently in the English language get the shortest sequences. To understand how Huffman coding algorithm works, consider the word Almost all websites use UTF-8 character encoding to represent letters and symbols. Every ASCII character in UTF-8, which also includes the English alphabet, uses 1 byte (8 bits). A 10-character string like The Huffman coding algorithm uses this knowledge to losslessly compress the string. It does this by generating a binary tree with every unique letter as a leaf. Letters with the lowest frequency (e.g. B, P, R) will find themselves at the bottom of the tree, while those that appear frequently (e.g. E, O, K) will ideally find themselves at the top. The topmost node in the tree is the root, and its value is equal to the total number of characters in the string. After generating the Huffman tree, all the left-branching and right-branching arrows are given 0 and 1 numbers respectively. You can then generate the Huffman code for any character by tracing the root-to-leaf path and joining all the 0s and 1s. You can notice that letters with the highest frequency have Huffman codes with the smallest bit sizes. Note: The Huffman coding algorithm can generate alternative binary codes by using a different ordering strategy for characters with the same frequencies. However, the total size of the encoded string will remain the same. Thatâs a 68.75% reduction in memory required to store the original word. Using the Huffman tree with the 0/1 convention generates binary codes that satisfy the prefix property. It ensures that the Huffman code of any specific character isnât a prefix of any other characterâs code, making it easy to decode the encoded string using the Huffman tree. This plays a primary role in GZIP decompression speed. Just like with the word above, the GZIP compressor uses Huffman coding algorithm to further optimize the tuples generated by the LZ77 algorithm. This results in highly compressed files with .gz extension. If youâre interested in learning more about how GZIP works, refer to this video for a quick overview. How Good Is GZIP Compression?Typically, GZIP achieves a compression ratio of around 70% for small files, but it can reach up to 90% for larger text-based assets. In the above table, you can see that compressing minified files with GZIP can reduce their size further. Note: You can compress any file type with GZIP, but for assets already compressed with other methods (e.g. images, videos), itâll not amount to any savings. Sometimes, it may even increase the file size. The strength of a compression algorithm doesnât just depend on its compression ratio, but also how quickly and efficiently it can compress and decompress data. Thatâs where GZIP excels for most use cases. Since GZIP decompresses swiftly using a streaming algorithm, itâs a great fit for web protocols where speed is of the essence. Plus, GZIP uses minimal resources to both compress and decompress data, making it ideal for both servers and clients alike. We grew our traffic 1,187% with WordPress.
|
AuthorEmpreendedor digital, blogueiro e ativista ambiental. Arquivos:
November 2020
Categorias
|