简体   繁体   English

包装Javascript-值得吗?

[英]Packing Javascripts - Is it worth it?

If a packed Javascript saves 20kb off the download size, but takes a perfomance hit to unpack, then are there any benefits, besides obfuscating the code? 如果打包的Javascript节省了20kb的下载量,但需要性能出色的解压缩功能,那么除了使代码混乱之外,还有什么好处?

I've made this a community wiki because it may be open to some discussion. 我将其作为社区Wiki,因为它可能需要进行一些讨论。

Packed javascript code generally does not take longer to execute vs regular code. 与常规代码相比,打包的javascript代码通常不需要花费更长的时间来执行。 Most code packers will shorten variables, function names and use a host of tricks to make the source smaller. 大多数代码打包器都会缩短变量,函数名称,并使用许多技巧来缩小源代码。 The resulting source is fully executable! 结果源是完全可执行的! You can verify this by taking a look at it during runtime with firebug. 您可以通过在运行时使用Firebug进行查看来验证这一点。 You'll see the compressed code executing in its minimized form. 您将看到压缩的代码以其最小化形式执行。

简短的回答,是的,因为客户端计算机的拆包时间比传输速度快,并且互联网本身也过载,所以任何使事情变得更好的贡献都受到匿名的赞赏,还请记住,大多数客户端将缓存这些东西,并且更大的客户端归档文件的机会越大,它就会从客户端缓存中撤出自身或其他内容,尤其是在移动设备中

Presumably, packed JavaScript is (slightly) more efficient for the server to send, while the unpacking expense is absorbed by the client. 据推测,打包的JavaScript对于服务器的发送效率(略)更高,而打包费用则由客户端承担。

If the user experience is equivalent in both cases, I'd go for the packed JS. 如果两种情况下的用户体验都相同,我会选择打包的JS。

Anytime you can push some work to the client without incurring a negative user experience, go for it, and reap the benefits of distributed computing. 只要您可以在不引起负面用户体验的情况下,就可以将一些工作推销给客户端,那就去做,并从分布式计算中受益。

The guys at Yahoo's YSlow recommend using both a minifier and gzip compression. 雅虎YSlow的专家建议同时使用压缩程序和gzip压缩程序。

The minifier will strip out whitespace, shorten variable names etc. That way you can code with proper indentation and variable names so that other developers can understand your code. 压缩程序将去除空格,缩短变量名等。这样,您可以使用适当的缩进和变量名进行编码,以便其他开发人员可以理解您的代码。

The gzip compression is probably even more valuable. gzip压缩可能更有价值。

The question is, why does the packing save 20kB? 问题是,为什么包装节省20kB? You should investigate making your 'normal' javascript smaller. 您应该研究使“普通” JavaScript变小。 Is the readability of your code significantly improved by putting spaces around operators, or by using very long variable and function names? 通过在运算符周围放置空格或使用非常长的变量和函数名称,是否可以显着提高代码的可读性? The most egregious waste of bandwidth I have seen on any web content, script or page or css, is indenting with spaces. 我在任何Web内容,脚本,页面或CSS上看到的最浪费带宽的问题是缩进空格。 If you must indent, use tabs, or single spaces. 如果必须缩进,请使用制表符或单个空格。 Nothing is more wasteful than a hundred lines in a row with 20+ spaces at the beginning. 没有什么比排成一排的开头有20多个空格的浪费更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM