Which javascript minification library produces bet

2019-01-12 03:24发布

问题:

Between Yahoo! UI Compressor, Dean Edwards Packer and jsmin, which produces better results, both in terms of resulting footprint and fewer errors when obfuscating.

回答1:

Better is a bit subjective here, since there are multiple factors to consider (even beyond those you list):

  1. Compressed size doesn't tell the whole story, since an aggressive compressor can result in slower run-time performance due to the additional time needed to run unpacking code prior to browser interpretation.
    • Errors are easiest to avoid when you control the input code - judicious use of semicolons goes a long way. Run JSLint over your code, and fix any problems reported.
    • The style and size of the code itself will affect the results, of course.
    • And finally, it's worth keeping in mind that server-side gzip compression will always result in a smaller download than any code compression, although some code compression tools will combine with gzip more effectively.

My recommendation is to run the code you intend to compress through several compressors (an automated comparison tool such as CompressorRater helps...), and choose based on the results - remembering to test, profile and compare the actual page load times afterward.



回答2:

A great way to compare the best compressors is The JavaScript CompressorRater by Arthur Blake.

What you're usually interested in is the size after compressing with GZIP (you should configure your web server to perform the compression).

The best results are usually from the YUI Compressor or Dojo ShrinkSafe. The differences were so small that after a while I stopped comparing and I just use the YUI Compressor.

EDIT: since the original time this question was asked, 2 new minifiers have been released. They're both usually at least as good as, if not better than, the YUI Compressor.

  • Google's Closure Compiler. Includes an aggressive advanced optimization mode that's sometimes applicable.
  • Microsoft's Ajax Minifier (search this page for "minifier")

EDIT 2:

  • UglifyJS, chosen by the jQuery team for the official 1.5 release


回答3:

Definitely check out Dojo Shrinksafe. It was reworked recently and apparently the performance has been improved.



回答4:

Full disclosure, I'm behind this: http://www.toptensoftware.com/minime which does minification, obfuscation and a reasonable set of lint style checks. Currently it produces smaller output than Yui, not quite as good as Closure.



回答5:

This is an old question and the Google Closure Compiler didn't exist then. I haven't used it yet, but it looks really good.



回答6:

As a Mootools user, I notice that Mootools has replaced Dean Edwards' Packer by YUI Compressor. I also remember there was a discussion on Ajaxian.com where Julien (Compressor author) pointed out the areas where YUI Compressor did better. I used Compressor and have never seen any problem, but I have never studied to which produces fewer error when obfuscating.



回答7:

YUI Compressor compresses more safely and compactly than Packer does. I believe Packer needs the JavaScript to be perfectly formed otherwise it will cause a JavaScript error when the script is loaded. Still, regardless of which you use, you'll get the biggest performance increase by Gzipping your file.



回答8:

There's also a port of the YUICompress for .NET (which includes a build task for TFS) on Codeplex.