简体   繁体   English

网页上是否存在包括未使用的Java脚本或JQuery调用在内的性能/安全性惩罚?

[英]Is there a Performance/Security Penalty Including Unused Javascript or JQuery Calls on a Web Page

We are developing a master dynamic web template for our users. 我们正在为我们的用户开发一个主动态Web模板。 There are certain pages that may require calls to jquery-1.11.3.min.js for some functionality. 在某些页面上,可能需要调用jquery-1.11.3.min.js才能实现某些功能。 The question came up about including it in the master page so it is available on every page if needed. 出现了有关将其包含在母版页中的问题,因此如果需要,可以在每个页面上使用它。 I am curious if there is a performance or security penalty for doing this. 我很好奇这样做是否会影响性能或安全性。

It's considered a bad practice to include files that are unused. 包含未使用的文件被认为是不好的做法。 Even though they might be cached, they still take up memory. 即使它们可能被缓存,它们仍然占用内存。 If they are not cached then extra connection and trips are made over the wire and slow down your page. 如果未缓存它们,则会通过网络进行额外的连接和跳闸,从而降低您的页面速度。

I would recommend to use RequireJS for all your javascript dependency resolution 我建议对所有JavaScript依赖项解析都使用RequireJS

The first time it is requested, there may be a hit. 第一次请求时,可能会遇到问题。

However, because jQuery is used widely, the chances are that it will be cached at some point along the server request chain, and quite possibly in the user's browers anyway. 但是,由于jQuery被广泛使用,因此很有可能会在服务器请求链中的某个时刻缓存它,并且很可能在用户的浏览器中缓存它。 It's also minified, so the size is quite small. 它也被缩小了,所以尺寸很小。

That said, even if not cached, once it's been requested once, it will be cached in the users browser (almost certainly). 也就是说,即使不被缓存,一旦被请求一次,它也将被缓存在用户浏览器中(几乎可以肯定)。

Here are some interesting questions / articles for further reading / info: Yahoo is well known for contributing to general knowledge on speeding up websites - read about this here: http://developer.yahoo.com/performance/rules.html 以下是一些有趣的问题/文章,供您进一步阅读/了解信息:雅虎以促进网站加速方面的常识而著称-在此处了解有关此信息: http : //developer.yahoo.com/performance/rules.html

Google's equivalent: https://developers.google.com/speed/docs/insights/rules Google的等效版本: https//developers.google.com/speed/docs/insights/rules

The best answer here is really an opinion: your opinion . 最好的答案实际上是一种意见: 您的意见

This kind of thing is not necessarily bad practice; 这种事情不一定是不好的做法。 however, it can cause your website to slow down if the viewer has not cached the particular version of jQuery that you are using. 但是,如果查看器没有缓存您正在使用的特定版本的jQuery,它可能会导致您的网站速度降低。

The answer to your question is another question: are you willing to make that sacrifice in order to be lazy and not include the link in every page that requires jQuery? 您的问题的答案是另一个问题:您是否愿意为了懒惰而不是在需要jQuery的每个页面中都包含链接而做出牺牲?

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

相关问题 使用大量jQuery调用来缩小页面上的JavaScript性能问题? - Narrowing down JavaScript performance issues on a page using a lot of jQuery calls? 在页面中包含JavaScript和CSS(jQuery) - Including JavaScript and CSS in a page (jQuery) jQuery 的 live() 的性能损失 function - Performance penalty for jQuery's live() function 性能损失JavaScript回调函数 - Performance penalty JavaScript callback-functions 未使用的javascript函数会降低网页性能吗? - Can unused javascript functions slow down page performance? javascript或jquery代码以获取iframe中网页的整个文本内容(包括/排除该页面上的链接) - javascript or jquery code to obtain entire text content of web page within an iframe (including/excluding links on that page) 包括jquery破坏了我的简单网页 - Including jquery breaks my simple web page jquery / javascript代码获取网页主网页的全文内容(包括链接文本但不包括图片) - jquery/javascript code to obtain full text content of main web page(including links texts but excluding images) of a web page 用于在页面加载时调用的javascript函数的注册表(包括AJAX调用) - Registry for javascript functions that get called on page load (including AJAX calls) JavaScript,未使用的数组会影响性能吗? - JavaScript, will unused arrays affect performance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM