Javascript.代码的位置会影响性能吗? [英] Javascript. Does the position of the code affect performance?

查看:46
本文介绍了Javascript.代码的位置会影响性能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找提高 JavaScript 代码性能的方法和技巧.我想知道为了获得最佳性能而使用的最佳做法是什么.

I was searching for ways and tricks to improve my javascript code performance. I would like to know what are the best practice to use in order to have best performance.

脚本的位置(在标题中、在正文中、在代码中间还是在末尾)是否会影响性能?

Does the position of the script (in the header, in the body, in the middle of the code or at the end) affect the performance?

感谢您的建议.

推荐答案

如果JS很多,放在文档的最后.虽然这对加载时间没有影响,但用户会更快地看到页面,并且可以在 js 加载时开始阅读它,而不是在 JS 下载之前什么也看不到(当你把它放在头上时会发生什么).它只是使下载看起来更快.这也将解决上面提到的关于在未完成的文档上执行脚本的问题,尽管为此,更好的解决方案是使用 window.onload().

If there is a lot of JS, put it at the end of the document. Although this makes no difference on load time, the user will see the page sooner and can begin to read it while your js loads, as opposed to seeing nothing until your JS is downloaded (which is what happens when you put it in the head). It merely makes the download appear faster. This would also solve the problem mentioned above about the script executing on an unfinished document, although for that, an even better solution is to use window.onload().

这篇关于Javascript.代码的位置会影响性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆