yuicompressor和google closure簡單比較

以前用yuicompressor要壓縮js文件,google closure開源後,開發者們有多了一個選擇,作爲一個使用者,我對yuicompressor和google closure簡單的做了一下比較:

通常在項目裏都是使用腳本運行,使用web服務的很少,所以這裏採用的是下載jar包在命令行下運行,yuicompressor的包名爲yuicompressor-2.2.4.jar,google closure的包名爲:compiler.jar。

[b]比較一: google網站的例子[/b]
google網站提供了一個helloworld的例子,如下

// A simplest function.
function hello(longName) {
alert('Hello, ' + longName);
}
hello('New User');

分別用yuicompressor和google closure進行壓縮:

//yuicompressor
java -jar yuicompressor-2.2.4.jar hello.js -o yui_hello.js

//google closure
java -jar compiler.jar --js hello.js --js_output_file g_hello.js

結果如下:

// the javascript for g_hello.js
function hello(a){alert("Hello, "+a)}hello("New User");

// the javascript for yui_hello.js
function hello(A){alert("Hello, "+A)}hello("New User")

相信大家已經發現:使用yuicompressor壓縮的文件比使用google closure壓縮的文件少了一個分號,進行到這裏,我相信80%的開發人員會得出結論: 就是yuicompressor的壓縮能力比google closure強,而且我當時也是這麼想的。真是這樣嗎?

[b]再做一次試驗,這次使用jquery[/b]

//yuicompressor
java -jar yuicompressor-2.2.4.jar jquery-1.3.2.js -o yui_jquery-1.3.2.js

//google closure
java -jar compiler.jar --js jquery-1.3.2.js --js_output_file g_jquery-1.3.2.js

jquery文件大小是120,763字節,yui_jquery-1.3.2.js大小是56,835,g_jquery-1.3.2.js的大小是55,323字節,使用google closure壓縮的文件大小是yuicompressor的97.3%。產生的這個結果有點讓人苦笑不得,雖然不能據此就說google closure壓縮js的能力更強,至少在jquery上,google closure的能力體現了出來,相比之下,google網站上提供的例子就有些欠妥當,一個小小的分號,很可能流失很多用戶。

儘管如此,但是兩者的差異並不是十分明顯,考慮到yuicompressor可以壓縮css文件,yuicompressor仍然會是目前的首選,作爲個人,我喜歡google的產品,喜歡開源技術,所以我會持續關注google的相關產品。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章