简体   繁体   English

从Javascript中删除空白行会提高性能吗?

[英]Will removing blank lines from Javascript increase the performance?

I have a very large js file, approximately 1200 lines, and some of the code is written like this (with the spaces): 我有一个非常大的js文件,大约1200行,其中一些代码是这样写的(带空格):

function a () {



           alert("aaaa");



}


function b () {

                                                 alert("bbbb");

}

If I delete the empty lines, will it improve the speed of the web site? 如果删除空白行,是否可以提高网站速度? I have looked at other js files, for example open the source on google, and there is no empty lines and spaces. 我看过其他js文件,例如在google上打开源代码,并且没有空行和空格。 So tell me please will this improve the performance of the site? 那么请告诉我,这会改善网站的性能吗?

White spaces are not effect the performance of a javascript file. 空格不会影响javascript文件的性能。 But, if you have many characters of spaces (" ") the file size might be larger and the browser will have to load a larger file, which take more time. 但是,如果您有许多空格字符(“”),则文件大小可能会更大,并且浏览器将必须加载更大的文件,这会花费更多时间。

If you want to minimize your file as much as you can, just search google Javascript minifier . 如果您想最大程度地减少文件,只需搜索google Javascript minifier即可

Good Luck! 祝好运!

It will make it take a little less time for the script to reach the client, but the performance improvement will be so small as to be unnoticeable. 脚本到达客户端所需的时间会减少一些,但是性能提升将非常小,以至于不明显。

You should get rid of the blank lines anyway because that's weird. 无论如何,您都应该摆脱空白行,因为那很奇怪。

By removing surplus characters (blank spaces, carriage returns, comments etc), you are reducing the file size of the file, which means it can be downloaded quicker. 通过删除多余的字符(空格,回车符,注释等),可以减小文件的文件大小,这意味着可以更快地下载文件。

This process is called minification . 这个过程称为缩小 There are lots of tools on the internet to do this for you. 互联网上有很多工具可以帮助您完成此任务。

Minification (and related, obfuscation ) are best practises to speed up your website. 缩小(以及相关的混淆 )是加速网站的最佳做法

JavaScript will not perform any better or worse with additional whitespace. 附加空白不会使JavaScript表现得更好或更差。 The only gain in compressing a JS file is in reducing the loading time. 压缩JS文件的唯一好处是减少了加载时间。 A file that might originate as 1MB might be reducable to 100KB* which would be much faster to load. 最初为1MB的文件可能会缩减为100KB *,这将大大加快加载速度。

* example only *仅示例

For relatively small files or small amounts of whitespace the difference will not be huge but it does take slightly longer to parse and use slightly more memory. 对于相对较小的文件或少量的空白,差异不会很大,但是解析的时间会稍长一些,并且会使用更多的内存。 For desktop browsers it is largely negligible but it can end up being a pretty big deal for mobile browsers where the CPUs are a LOT slower and the memory is very limited. 对于台式机浏览器来说,它几乎可以忽略不计,但是对于移动浏览器来说,这可能是一个很大的问题,因为这些浏览器的CPU速度要慢很多,而且内存非常有限。

It can also impact the cacheability of the resources on mobile if they cross certain boundaries (and mobile browsers currently cache the resources uncompressed so it's the uncompressed size that matters). 如果它们跨越一定的边界,它也会影响移动设备上资源的可缓存性(移动浏览器当前缓存未压缩的资源,因此重要的是未压缩的大小)。

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

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