About JavaScript-gzip static compression method _javascript Tips

Source: Internet
Author: User
Tags gzip download cpu usage high cpu usage tomcat
One page reduction 10KB for a small site, is not what, but for large sites, the cumulative is a lot of traffic ... gzip compression is an effective and feasible approach, but requires Apache support (Apache has a common port solution with IIS).
Traditional JS Compression (delete annotations, delete extra space, etc.) to provide the compression rate is sometimes not the case, thanks to the current browser support compressed transmission (by setting the HTTP header content-encoding=gzip), you can through the server configuration (such as Apache) Provide compression for your JS or gzipfilter in Appfuse to enable Tomcat to provide this capability
Now the problem is that this dynamic compression will lead to high CPU usage, now I think of the resolution by providing static compression (that is, JS through the Gzip.exe compressed)

The following describes the application in Tomcat

1. Save the Prototype.js through Gzip.exe compression into Prototype.gzjs 2. Set header, I wrote a simple addheadersfilter to add all the files to the end of Gzjs to set header Configuration in the Content-encoding=gzip web.xml
Copy Code code as follows:
<filter>
<filter-name>AddHeaderFilter</filter-name>
<filter-class>
Badqiu.web.filter.AddHeaderFilter
</filter-class>
<init-param>
<param-name>headers</param-name>
<param-value>Content-Encoding=gzip</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>AddHeaderFilter</filter-name>
<url-pattern>*.gzjs</url-pattern>
</filter-mapping>
Code to test whether Prototype.js is normal

Copy Code code as follows:
<!--type= "Text/javascript" must be few, some browsers lack this can not run, specifically have forgotten-->
<script src= "Prototype.gzjs" type= "Text/javascript" ></script>
<body>
<input id= "username" name= "username" value= "Badqiu"/><br/>
<input id= "Email" value= "badqiu@gmail.com"/>
<script>
<!--test prototype method is normal-->
Alert ($F (' username '))
</script>
</body>

In Apache, you can map the header of the. gzjs file directly by adding addencoding X-gzip. Gzjs to the httpd.conf.

Two. Compression rate

Prototype.js 1.5.0_rc0 Original Size 56KB, without any processing directly using gzip compression to 12KB, total compression rate 79%
2. Through the JS compression tool compressed Protytype.js for 20KB, using gzip compression to 10KB, the total compression rate of 83%
3. The actual project of multiple JS merged into the file 439KB, directly through gzip compression to 85KB, the total compression rate of 81%

4 439KB through JS compression to 165KB, and then after gzip compression to 65KB, the total compression rate of 86%

Basically you can ignore the JS compression tool compression rate, directly using gzip compression
gzip Download Address http://www.gzip.org

Sample compression configuration for tomcat download address: Http://www.blogjava.net/Files/badqiu/gziptest.rar
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.