在 gzip 压缩之前缩小 JavaScript 有什么好处吗?
在压缩之前缩小是否有一些有效的目的?如果首先缩小,gzip 压缩的文件似乎不太可能变小。
我问这个问题是因为在缩小的代码中诊断生产问题要困难得多,而且我想知道人们是否无缘无故地遭受这种情况。
Is there some valid purpose to minifying before compressing? It seems highly unlikely that the gzipped file is smaller if it's minified first.
I ask because diagnosing production problems in minified code is substantially more difficult, and I'm wondering if people are subjecting themselves to that for no purpose.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
至于原始文件大小,这里有一个示例(jQuery 1.4.2):
因此缩小版本的大小约为一半。
As for raw file size, here is a sample (jQuery 1.4.2):
So the minified version is about half the size.
是的,肯定有好处。
缩小是一种有损压缩,而 gzipping 是无损压缩。因此,通过缩小,您可以删除不需要的数据(例如注释和长变量名),这始终有助于缩小文件。即使使用 gzip,在大多数情况下仍然存在差异。
示例:
这可能会缩小为:
或者如果缩小器很智能:
所有代码都会给出相同的结果,但大小差异很大。
Yes, there is definately a benefit.
Minifying is a lossy compression whereas gzipping is lossless. Ergo, with minifying you remove unneeded data (like comments and long variablenames) which will always help to make your file smaller. Even with gzip there will still be a difference in most cases.
Example:
That might be minified to:
Or if the minifier is smart:
All code gives the same results, but the size differs a lot.
或许。除了删除空格之外,缩小 JavaScript 可能会导致相同文本的更多重复,这可能意味着 gzip 的压缩率略高。幸运的是,自从使用
gzip
命令行工具(在 *nix 和 适用于 Windows 使用相同的压缩算法(尽管格式不完全相同)。Maybe. Beyond the removal of whitespace, minifying JavaScript may lead to more repetitions of the same text, which may mean slightly higher compression with gzip. Fortunately it's easy to do a before-and-after since the
gzip
command line tool, common in *nix and available for Windows uses the same compression algorithm (although not exactly the same format).它还可以帮助加快浏览器中 JavaScript 代码的解析速度。根据文件的大小,浏览器可能会花费大量时间来解析和标记文件,这些时间将通过缩小来减少。
当然,只有基准测试和分析才能告诉您这是否真的对您的特定情况有好处。
我发现最有效的方法是在我的网站上保留所有 .js 文件的缩小版本和非缩小版本,然后仅使用配置开关在两者之间切换。这样,正常生产就可以使用缩小版本,然后如果我必须调试某些内容,只需翻转开关,就会提供非缩小版本。 (当然,构建过程确保缩小版本和非缩小版本同步)
It can also help to speed up parsing of the javascript code in the browser. Depending on the size of your files, the browser may spend significant amounts of time parsing and tokenising the file which will be reduced by minifying.
Of course, only benchmarking and profiling will tell you if that's actually going to be a benefit for your particular situation.
What I find works best is I keep both minified and non-minified versions of all my .js files on my website and just use a configuration switch to switch between the two. That way, normal production can use the minified version and then if I have to debug something, just flip the switch and the non-minified version is served up instead. (The build process ensures that the minified and non-minified versions are in sync, of course)
当我在 gzip 之前缩小时,我总是看到最终字节数明显减少。
我有一个 20 分钟的 hack job php 脚本,它与 yui 压缩器和 googles 闭包编译器交互。它显示了我之前和之后的字节,包括 gzip 之后的字节,所以我很容易检查。
I've always seen noticeable reduction in the final number of bytes when I minify before gzip.
I have a 20 minute hack job php script that interfaces with yui compressor, and googles closure compiler. It shows me before and after bytes including after gzip, so pretty easy for me to check.