在 gzip 压缩之前缩小 JavaScript 有什么好处吗?

发布于 2024-08-27 18:33:00 字数 110 浏览 7 评论 0原文

在压缩之前缩小是否有一些有效的目的?如果首先缩小,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

揽清风入怀 2024-09-03 18:33:00

至于原始文件大小,这里有一个示例(jQuery 1.4.2):

$ curl http://code.jquery.com/jquery-1.4.2.js | gzip > jquery.gz
$ curl http://code.jquery.com/jquery-1.4.2.min.js | gzip > jquery-min.gz

$ ls -la jquery*
-rw-r--r--  1 me  staff  24545 Apr  7 12:02 jquery-min.gz
-rw-r--r--  1 me  staff  45978 Apr  7 12:02 jquery.gz

因此缩小版本的大小约为一半。

As for raw file size, here is a sample (jQuery 1.4.2):

$ curl http://code.jquery.com/jquery-1.4.2.js | gzip > jquery.gz
$ curl http://code.jquery.com/jquery-1.4.2.min.js | gzip > jquery-min.gz

$ ls -la jquery*
-rw-r--r--  1 me  staff  24545 Apr  7 12:02 jquery-min.gz
-rw-r--r--  1 me  staff  45978 Apr  7 12:02 jquery.gz

So the minified version is about half the size.

怂人 2024-09-03 18:33:00

是的,肯定有好处。

缩小是一种有损压缩,而 gzipping 是无损压缩。因此,通过缩小,您可以删除不需要的数据(例如注释和长变量名),这始终有助于缩小文件。即使使用 gzip,在大多数情况下仍然存在差异。

示例:

function foo(this_is_my_variable){
    var this_is_my_other_variable = 0;
    this_is_my_other_variable = this_is_my_other_variable + this_is_my_variable;
    return this_is_my_other_variable;
}

这可能会缩小为:

function foo(a){
    var b = 0;
    b = b +a;
    return b;
}

或者如果缩小器很智能:

function foo(a){
    return a;
}

所有代码都会给出相同的结果,但大小差异很大。

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:

function foo(this_is_my_variable){
    var this_is_my_other_variable = 0;
    this_is_my_other_variable = this_is_my_other_variable + this_is_my_variable;
    return this_is_my_other_variable;
}

That might be minified to:

function foo(a){
    var b = 0;
    b = b +a;
    return b;
}

Or if the minifier is smart:

function foo(a){
    return a;
}

All code gives the same results, but the size differs a lot.

无名指的心愿 2024-09-03 18:33:00

或许。除了删除空格之外,缩小 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).

淡墨 2024-09-03 18:33:00

它还可以帮助加快浏览器中 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)

江心雾 2024-09-03 18:33:00

当我在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文