Home >Web Front-end >JS Tutorial >About JavaScript's gzip static compression method_javascript tips

About JavaScript's gzip static compression method_javascript tips

WBOY
WBOYOriginal
2016-05-16 19:21:531227browse

For a small website, reducing a page by 10KB is nothing, but for a large website, it adds up to a lot of traffic... gzip compression is an effective and feasible method, but it requires Apache Support (Apache and IIS share a port solution).
The compression rate provided by traditional JS compression (deleting comments, deleting extra spaces, etc.) is sometimes unsatisfactory. Fortunately, modern browsers support compressed transmission (by setting the Content-Encoding=gzip of the http header). You can pass The server configuration (such as apache) provides compressed transmission for your js, or the GZipFilter used in appfuse allows tomcat to also provide this capability
The problem now is that this dynamic compression will cause the CPU usage to be too high. Now The solution I think of is to provide static compression (that is, js is pre-compressed through gzip.exe)

1. The application in tomcat is described below

1. Convert prototype.js Compress and save to prototype.gzjs through gzip.exe 2. Set header, I wrote a simple AddHeadersFilter to add all files ending with gzjs and set header Content-Encoding=gzip configuration in web.xml

Copy code The code is as follows:
🎜>
badqiu.web.filter.AddHeaderFilter


                                                                                                                                                               -name>
                                                                                                                          
AddHeaderFilter
                                                                                           The code




Copy the code

The code is as follows:
                                                                                                                                     OK -->
alert($F('username'))




In Apache You can directly map the header of the .gzjs file by adding AddEncoding Any processing directly uses gzip to compress to 12KB, with a total compression rate of 79%
2. The prototype.js compressed by the js compression tool is 20KB, and uses gzip to compress to 10KB, with a total compression rate of 83%
3. Actual Multiple js files in the project were merged into a 439KB file, which was directly compressed by gzip to 85KB, with a total compression rate of 81%

4. 439KB was compressed by js to 165KB, and then compressed by gzip to 65KB, with a total compression rate of 86 %

Basically you can ignore the compression rate of the js compression tool and use gzip compression directly
gzip download address
http://www.gzip.org


Tomcat compression configuration example download address:
http://www.blogjava.net/Files/badqiu/gziptest.rar

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn