js压缩工具(转载)

怎么压缩Js?为什么要压缩Javascript? Javascript compressed/crunched
开发Ajax除了自己要写js代码外,同样也免不了要使用其它第三方js库,是否该为JavaScript减肥?
当你提高了用户体验,做出了很绚丽的效果而欣喜的时候,是否想过优化一下JS的效率,比如微软的live,其中的js做了压缩处理。JS的速度分为两种,JS下载速度和Js执行速度。

今天就先来说说JS下载速度。要想js的下载速度快,就需要尽量减小js文件的大小。

两种Js压缩工具(Javascript压缩工具)

Javascript compressed – YUI Compressor

According to Yahoo!’s Exceptional Performance Team, 40% to 60% of Yahoo!’s users have an empty cache experience and about 20% of all page views are done with an empty cache。

The goal of JavaScript and CSS minification is always to preserve the operational qualities of the code while reducing its overall byte footprint (both in raw terms and after gzipping, as most JavaScript and CSS served from production web servers is gzipped as part of the HTTP protocol). The YUI Compressor is JavaScript minifier designed to be 100% safe and yield a higher compression ratio than most other tools. Tests on the YUI Library have shown savings of over 20% compared to JSMin (becoming 10% after HTTP compression). Starting with version 2.0, the YUI Compressor is also able to compress CSS files by using a port of Isaac Schlueter’s regular-expression-based CSS minifier。

http://developer.yahoo.com/yui/compressor/

Download the YUI Compressor. 
http://www.julienlecomte.net/yuicompressor/

YUI Compressor最新版本下载 – YUI Compressor 2.4.1下载地址:
http://www.julienlecomte.net/yuicompressor/yuicompressor-2.4.1.zip

JS和CSS压缩工具图形界面版本下载地址
淘宝团队对YUICompressor做了层简单的封装,称之为TBCompressor.

http://lifesinger.org/blog/wp-content/uploads/2008/10/TBCompressor_v2.4.zip

淘宝是怎么压缩js和css的,淘宝使用的就是YUI Compressor
目前Jquery从1.3版本开始也使用YUICompressor

引用
The YUI Compressor is a JavaScript compressor which, in addition to removing comments and white-spaces, obfuscates local variables using the smallest possible variable name. This obfuscation is safe, even when using constructs such as ‘eval’ or ‘with’ (although the compression is not optimal is those cases) Compared to jsmin, the average savings is around 20%.
The YUI Compressor is also able to safely compress CSS files. The decision on which compressor is being used is made on the file extension (js or css)




Javascript compressed – ESC Introduction 

这里我们可以使用一个工具ESC(ECMAScript cruncher)来帮我们完成这个工作,不过这个工具只能在Windows下使用。到http://www.saltstorm.net/depo/esc/introduction.wbm?pod=js下载ESC.zip,解压后看看它的帮助文档。很简单。

ESC is an ECMAScript pre-processor written in JScript, enabling an unlimited number of external scripts to be compressed/crunched into supertight, bandwidth-optimized packages. Featuring several compression-techniques such as comment removal, whitespace stripping, newline stripping and variable substitution ESC can reduce the overall size of your code with up to ~45%. Single, multiple scripts and even directories with scripts can be merged together at the compression level you decide. The processed output can later be appended or written to a file, or piped to another application for further processing via STDOUT. 
  
ESC do NOT support crunching of inline scripts. So any attempt passing HTML, ASP, JSP, PHP or other equivalent documents to ESC is done at your own risk.  
  
ESC supports four levels of compression, where a higher level equals higher compression. Beware though that levels >2 requires your code to be syntaxically perfect or ESC will punish you by producing a broken and useless output.  
  
The compression ratio should hit around 25% using the default compression level on a vanilla looking script, but results as high as ~45% can be achieved depending on the script’s design / your style of writing code.  
  
压缩级别分为5种,从0到4

Level 0 :: No compression

Level 1 :: Comment removal

Level 2 :: Whitespace removal

Level 3 :: Newline removal

Level 4 :: Variable substitution

在WINDOWS命令行下执行

cscript ESC.wsf -ow menu2.js menu.js将会把menu.js按照js压缩级别2来压缩(默认js压缩级别为2)为menu2.js

cscript ESC.wsf -l 3 -ow menu3.js menu.js将会把menu.js按照js压缩级别3来压缩为menu3.js

需要注意的是,js压缩级别4会把变量名修改,如果你的js中用到了全局变量或者类的话,就不能使用该压缩级别了,否则其它使用你的js的文件可能会无法正常运行。

试了一下,把yui的menu.js压缩了一下,对应的级别和压缩率分别如下:

js压缩级别1:压缩率44.41%

js压缩级别2:压缩率62.82%

js压缩级别1:压缩率64.93%

原来130多K的js文件压缩后也就40多K,看来还是挺有用处的。

另外还有一个小工具jsmin也可以压缩,不过没有ESC的level选项,看了一下好像压缩效果类似于ESC的level3.济南大学的一些老师写了个jsmin的图形界面程序JsMinGUI,使用起来更加方便。:-)

Dojo项目组也提供了一个工具,shrinksafe可以通过http://alex.dojotoolkit.org/shrinksafe/在线的上传要压缩的文件,处理完毕后可以保存到本地,另外shrinksafe还提供了一个本地运行的版本,需要jre1.4的支持。

Ps:对于一些压缩的代码,在阅读时可以用JavaScript Code Improver来进行格式化。看看它主页上的一个例子:
javascript压缩代码示例
原来的代码:

<script language=”JavaScript”>var i=0,s=””,k=0;function foo(){for(j=0;j<10;j++){for(i=0;i<10;i++){s=”string1″;k=Math.floor(Math.random()*10);}for(i=20;i>9;i–){s=”string2″;k=I;}}}</script>



格式化后:

<script language=”JavaScript”>
var I = 0, s = “”, k = 0;
function foo()
{
for(j = 0; j < 10; j++) 
{
for(I = 0; I < 10; i++) 
{
s = “string1″;
k = Math.floor( Math.random()*10 );
}
for(I = 20; I > 9; i–) 
{
s = “string2″;
k = I;
}
}
}
</script>




js压缩,巨NB的dHTML特效. 来自:
http://hometown.aol.de/_ht_a/memtronic/MemTronic_CruncherCompressor_v09g.html

JS压缩示例样本:JS压缩示例样本


更多的JS压缩器以及混淆器

In terms of code minification, the most widely used tools to minify JavaScript code are Douglas Crockford’s JSMIN, the Dojo compressor and Dean Edwards’ Packer:

Douglas Crockford’s JSMIN
http://crockford.com/javascript/jsmin

the Dojo compressor
http://dojotoolkit.org/docs/shrinksafe

Dean Edwards’ Packer
http://dean.edwards.name/download/

http://dean.edwards.name/packer/   在线Js压缩
A Online JavaScript Compressor/Obfuscator version 2.0.2
Also available as a .NET application
开源下载:
http://dean.edwards.name/download/#packer
JS压缩器帮助:http://dean.edwards.name/packer/usage/
http://dean.edwards.name/packer/usage/sample.html

http://hometown.aol.de/_ht_a/memtronic/

JavaScript Compressor/Obfuscator Demo:
http://www.brainjar.com/js/crunch/demo.html 

JavaScript Code Improver:
JavaScript Code Improver Download


前面提到的Javascript压缩,都是采用删除注释,删除多余空格,替换字符等方式进行的,还有一种通过服务器的配置来实现Js静态压缩的方法:
关于JavaScript的gzip静态压缩方法
传统的JS压缩(删除注释,删除多余空格等)提供的压缩率有时还是不尽不意,幸亏现在的浏览器都支持压缩传输(通过设置http header的Content-Encoding=gzip),可以通过服务器的配置(如apache)为你的js提供压缩传输,或是appfuse中使用的GZipFilter使tomcat也提供这种能力

现在的问题是这种动态的压缩会导致服务器CPU占用率过高,现在我想到的解决辨法是通过提供静态压缩(就是将js预先通过gzip.exe压缩好)

一.下面描述在tomcat中的应用

1.将prototype.js通过gzip.exe压缩保存成prototype.gzjs 
2.设置header,我编写了一个简单的AddHeadersFilter来将所有以gzjs结尾的文件增加设置header Content-Encoding=gzip 
web.xml中的配置 

<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>



测试prototype.js是否正常的代码 

<html>  
<head>  
<!– type=”text/javascript”不可少,有的浏览器缺少这个不能运行 –>  
<script src=”prototype.gzjs” type=”text/javascript”></script>  
</head>  
<body >  
     <input id=”username” name=”username” value=”badqiu”/><br />  
     <input id=”email” value=”badqiu@gmail.com”/>  
<script>  
     <!– 测试prototype的方法是否正常–>  
     alert($F(‘username’))   
</script>  
</body>  
</html>



在Apache httpd中可以直接通过在httpd.conf增加AddEncoding x-gzip .gzjs来映射.gzjs文件的header

二.相关压缩率数据 
1. prototype.js 1.5.0_rc0原始大小56KB,未经任何处理直接使用gzip压缩为12KB,总压缩率79% 
2. 通过js压缩工具压缩过的protytype.js为20KB,使用gzip压缩为10KB,总压缩率为83% 
3. 实际项目中的多个js合并成的文件 439KB,直接通过gzip压缩为85KB,总压缩率81% 
4. 439KB经过js压缩为165KB,再经过gzip压缩为65KB,总压缩率86%

基本上你都可以忽略js压缩工具的压缩率,直接使用gzip压缩

gzip下载地址 http://www.gzip.org 
tomcat的压缩配置示例下载地址: 
http://www.blogjava.net/Files/badqiu/gziptest.rar

其主要是利用服务器自带的动态压缩功能 传输时压缩数据流.

Ajax与系列精华Javascript库(Modello)
广告图片轮显-广告图片轮流显示
AJAX+POPUP+数据库,实现动态无限级栏目
JacaScript 通用表单验证函数(我佛山人)——改进版
多级关联菜单数据库版(老问题精解)
Css house—Css房子—Css高手境界
用CSS和JS画出来的美女(酷)
display与visibility的详细文档说明
实例分析CSS属性Display与Visibility不同
Flash CSS Box Model(CSS 盒模型)
CSS中的滑动门技术
CSS实现完美垂直居中
面向对象的JavaScript举例
使用this指针与prototype实现面向对象时的区别
FrameSet 与 Iframe 彻底剖析

通用滚动条配色工具(滚动条的CSS)
Javascript 压缩工具合集(Javascript compressed,Js压缩)
CSS巧用expression来区分只读文本框
CSS如何使DIV层水平居中(包括水平/垂直)
Javascript和弹出窗口
Web 2.0 编程思想:16条法则(Dion Hinchcliffe)
Ajax跨域工具: Modello.ajax

Javascript压缩工具(Javascript compressed,Js压缩)

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>