javascript javascript文件“太大”有多大

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3310746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 00:49:01  来源:igfitidea点击:

How big is "too big" for a javascript file

javascript

提问by Edward Shtern

With all the awesome jQuery plug-ins out there, it's so tempting to just start including them all over the place to enhance the look of our website.

有了所有很棒的 jQuery 插件,我们很想开始在所有地方包含它们以增强我们网站的外观。

I'm looking for some opinions on whether and when I should be concerned about the sizes of these scripts.

我正在寻找关于是否以及何时应该关注这些脚本的大小的一些意见。

Assume they're being cached on the web server level, so I guess the potential concerns are on bandwidth for the scripts being served from the web server, as well as performance on the client browser that needs to download all these scripts.

假设它们被缓存在 web 服务器级别,所以我猜潜在的问题是从 web 服务器提供的脚本的带宽,以及需要下载所有这些脚本的客户端浏览器的性能。

回答by Yuval Adam

There are two issues here as you mentioned.

正如你提到的,这里有两个问题。

The first is server load. If this is an issue, you can use the Google AJAX Libs APIs, and get your libs directly from Google's CDN.

首先是服务器负载。如果这是一个问题,您可以使用Google AJAX Libs APIs,并直接从 Google 的 CDN 获取您的库。

Second issue is client side loading time. Well, you should decide who your target audience is, and how much you are wiling to let the user wait. As a rule of thumb, most websites today consume ~300KBof bandwidth per page, including scripts, stylesheets and images.

第二个问题是客户端加载时间。好吧,你应该决定你的目标受众是谁,以及你愿意让用户等待多少。根据经验,当今大多数网站都会消耗~300KB每页的带宽,包括脚本、样式表和图像。

回答by Cesar Canassa

I don't think you should worry too much about it. Usually, the images will consume most of your bandwidth and not Javascript files.

我觉得你不用太担心。通常,图像会占用您的大部分带宽,而不是 Javascript 文件。

If you want to speed up your site, reducing the number of HTTP requests in your page is the place to start. This will probably give a much better performance boost. Try compressing all your .js files into just one and use CSS sprite techniques to display your images.

如果您想加速您的网站,减少页面中 HTTP 请求的数量是开始的地方。这可能会带来更好的性能提升。尝试将所有 .js 文件压缩为一个文件,并使用 CSS sprite 技术来显示您的图像。

回答by Mark

You have to put it in perspective. See how long the loading your page is and what percentage of that is javascript. Usually it's not that big.

你必须正确看待它。查看加载页面的时间以及其中 javascript 的百分比。通常它不会那么大。

Since js usually adds niceties to a site, it might not matter if it loads last, or relatively slowly, so long as it does not block other elements from being loaded.

由于 js 通常会为站点添加一些细节,因此它是持续加载还是相对缓慢加载可能无关紧要,只要它不阻止其他元素的加载即可。

In the way I see it, if you need the functionality, you'll have to load the code so it's unavoidable. If it's decorative, then load that decorative js last, and it's better to have it a bit later than to not have it at all.

在我看来,如果您需要该功能,则必须加载代码,因此这是不可避免的。如果是装饰性的,那么最后加载那个装饰性的js,晚点拥有总比完全没有好。

回答by Chris Marisic

As others have said for jQuery itself it is best to use any public CDN hosted copy, I would also recommend looking at using a tool to bundle your plugins / custom scripts into a package(s) that make sense. These will vary depending on your server type but 2 common tools are ASP.NET ScriptManager Control - Script Combining - What's the big deal?and Bundler - Bundler Now Supports Css And .less. On the latter post there is more information about this topic in general.

正如其他人对 jQuery 本身所说的那样,最好使用任何公共 CDN 托管副本,我还建议您考虑使用一种工具将您的插件/自定义脚本捆绑到一个有意义的包中。这些将根据您的服务器类型而有所不同,但 2 个常用工具是ASP.NET ScriptManager Control - Script Combining - 有什么大不了的?Bundler - Bundler 现在支持 Css 和 .less。在后一篇文章中,一般有关于这个主题的更多信息。

Edit:The current solution I use with .NET is http://getcassette.net/Cassette provides the best story for ASP.NET development, you store only .debug. copies of JS files in solution, the javascript files easily get intellisense, when you run in debug you get all of the debug js files out, and when you run with debug=false the get combined and minified. In both scenarios they have cache busting strings so you never have to worry about shift+refresh to not be using stale javascript.

编辑:我在 .NET 中使用的当前解决方案是http://getcassette.net/Cassette 为 ASP.NET 开发提供了最好的故事,您只存储 .debug。解决方案中的 JS 文件副本,javascript 文件很容易获得智能感知,当您在调试中运行时,您将获得所有调试 js 文件,而当您使用 debug=false 运行时,这些文件将被合并和缩小。在这两种情况下,它们都有缓存破坏字符串,因此您永远不必担心 shift+refresh 不会使用过时的 javascript。

回答by Quentin

How long is a piece of string?

一段绳子有多长?

It depends on the bandwidth of your target audience, the bandwidth of the server(s), and the relative value the JS provides.

这取决于目标受众的带宽、服务器的带宽以及 JS 提供的相对价值。