简体   繁体   English

在单个页面上加载jQuery是否有助于整个网站页面加载时间?

[英]Will loading jQuery on individual pages be helping with overall website pages load time?

If jQuery is only needed on certain pages, will loading jQuery on these individual pages be helpful with overall website pages load time? 如果只在某些页面上需要jQuery,那么在这些单独的页面上加载jQuery会对整个网站页面加载时间有帮助吗?

Given browser will cache javascript, it won't make much difference. 鉴于浏览器将缓存javascript,它不会有太大的区别。 But overall speaking, will it? 但总的来说,会吗? Or at least it doesn't hurt to do that. 或者至少这样做并没有什么坏处。 Right? 对?

You should never import a library that you don't use. 您永远不应导入不使用的库。

This being said 这就是说

  • If you're worried about your server workload : Use Google's CDN version . 如果您担心服务器工作负载:请使用Google的CDN版本
  • If you're worried about browser's loading time : Don't. 如果你担心浏览器的加载时间:不要。 If you use jQuery on other pages, it will be cached by the browser. 如果您在其他页面上使用jQuery,它将由浏览器缓存。
  • If you're worried about browser's execution time : Don't. 如果你担心浏览器的执行时间:不要。 JQuery initialization is much too fast to notice. JQuery初始化太快了,无法注意到。

That's correct, loading the jQuery only on pages which require it will help performance for the pages that don't use jQuery. 这是正确的,只在需要它的页面上加载jQuery将有助于不使用jQuery的页面的性能。 However, if a page containing the jQuery library has already been viewed, this won't make much of a difference as the javascript file will have already been cached locally. 但是,如果已经查看了包含jQuery库的页面,则这不会产生太大的差别,因为javascript文件已经在本地缓存。

Generally, unless you are only using jQuery on 1% if your pages, I don't think it hurts to put jQuery in the head of every page. 一般情况下,除非你的页面只使用1%的jQuery,否则我认为把jQuery放在每个页面的head都不会很痛。

If you are concerned about performance, you could link to the script hosted on Google. 如果您担心性能问题,可以链接到Google上托管的脚本。 This has benefits because if another site the user has viewed has already downloaded this script, it will already be cached for your website. 这样做有好处,因为如果用户查看过的其他网站已经下载了此脚本,则该网站已经缓存了该脚本。

https://developers.google.com/speed/libraries/devguide https://developers.google.com/speed/libraries/devguide

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

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