简体   繁体   English

如何减少javascripts的加载时间?

[英]How can I reduce the load time of my javascripts?

I have the following code on my page: 我的页面上有以下代码:

<script src="/Scripts/common/layout/addAccessControls.js"></script>
<script src="/Scripts/common/layout/addAjaxControls.js"></script>
<script src="/Scripts/common/layout/addBodyControls.js"></script>
<script src="/Scripts/common/layout/addContentControls.js"></script>
<script src="/Scripts/common/layout/addThemeControls.js"></script>
<script src="/Scripts/common/layout/hashClick.js"></script>
<script src="/Scripts/common/layout/setSideBar.js"></script>

Is it correct that all the scripts will load one after another? 所有脚本将一个接一个地加载是否正确? If so is there a way I can make more than one load at once? 如果有的话,我可以一次制作多个负载吗?

Just to clarify the question a bit. 只是为了澄清一下这个问题。 I can minify these and concat and maybe they're already mimified and concated :-) What I am intersted to find out is the load process given these exact files. 我可以缩小这些并且连接,也许它们已经被模仿和连接:-)我有兴趣找出的是给定这些确切文件的加载过程。 Is it one file after the other. 它是一个接一个的文件。 Does the browswer do anything to parallel load. 浏览器是否对并行负载做了任何事情。 Thanks 谢谢

Well, first problem is that the scripts are loaded one after the other. 好吧,第一个问题是脚本是一个接一个地加载的。 Second problem is the number of requests. 第二个问题是请求的数量。 The more requests, the longer it takes. 请求越多,所需的时间越长。

Most simple approach would be to concat the files server-side into one single .js file so that you only have one request left. 最简单的方法是将文件服务器端连接成一个.js文件,这样你只剩下一个请求。 This will speed up things. 这样可以加快速度。

If you additionally minify the scripts, this will speed up things, too. 如果你另外缩小脚本,这也会加快速度。

Depending on what framework you use server-side there are various solutions on how to do this. 根据您使用服务器端的框架,有各种解决方案可以执行此操作。 Eg, for Node.js you can use Piler to do this at runtime. 例如,对于Node.js,您可以使用Piler在运行时执行此操作。

Or, of course, you can always do this manually, respectively via a build job. 或者,当然,您始终可以通过构建作业分别手动执行此操作。

PS: And, of course, you can use other mechanisms to load scripts files, such as dynamically adding script tags which will allow you to parallelize loading. PS:当然,您可以使用其他机制来加载脚本文件,例如动态添加脚本标记,这将允许您并行加载。 See http://blogs.msdn.com/b/kristoffer/archive/2006/12/22/loading-javascript-files-in-parallel.aspx for details. 有关详细信息,请参阅http://blogs.msdn.com/b/kristoffer/archive/2006/12/22/loading-javascript-files-in-parallel.aspx

You can use async command (HTML5): 您可以使用async命令(HTML5):

<script async src="slow.js"></script>

Dynamic scripts load: 动态脚本加载:

var script = document.createElement("script");
script.async = false;
script.src = url;
document.head.appendChild(script);

HTML5 Async Scripts HTML5异步脚本

Yes, they will be loaded and executed one after the other. 是的,它们将被一个接一个地加载和执行 And you can't change that. 你无法改变这一点。

But if you're concerned about performances you may be happy to learn that the download process is different and may be parallelized by the browser. 但是如果你担心表演,你可能会很高兴得知下载过程不同,可能会被浏览器并行化。

You can see it in Chrome by opening the network tab of the Chrome Developer Tools . 您可以通过打开Chrome开发者工具的网络标签在Chrome中看到它。

If you want to reduce the total time, the best solution is to concatenate all the files in one (and minify them if possible but the important operation is the concatenation). 如果要减少总时间,最好的解决方案是将所有文件连接在一起(如果可能,将其缩小,但重要的操作是连接)。

You can concatenate and minify them with tools like this . 您可以连接和使用一样的工具它们最小 If you have a Mac there is even an awesome app that automates this process called CodeKit 如果你有一台Mac,那么甚至还有一个很棒的应用程序可以自动执行这个名为CodeKit的过程

This is script blocking. 这是脚本阻止。 Its a browser feature. 它是一个浏览器功能。 Different browsers download different number of files in parallel. 不同的浏览器并行下载不同数量的文件。 Scripts will be loaded and executed after each other. 脚本将相互加载并执行。 You can read these articles by Steve Souders and Nicolas Zakas. 您可以阅读Steve Souders和Nicolas Zakas撰写的这些文章。 This could help you. 这可以帮到你。

http://www.stevesouders.com/blog/2009/04/27/loading-scripts-without-blocking/ http://www.nczonline.net/blog/2009/06/23/loading-javascript-without-blocking/ http://www.stevesouders.com/blog/2009/04/27/loading-scripts-without-blocking/ http://www.nczonline.net/blog/2009/06/23/loading-javascript-without-阻断/

Depending on the browser and the number of other files being downloaded (images, for example), you may have several JavaScript files being downloaded simultaneously by the order you put them. 根据浏览器和正在下载的其他文件的数量(例如图像),您可能会按照您下载的顺序同时下载多个JavaScript文件。

You can see this happening by using Firebug or Chrome's developer tools. 您可以使用Firebug或Chrome的开发人员工具来了解这种情况。 You'll notice that your browser will always load more than one file but the maximum number of simultaneous downloads varies on the browser and version being used. 您会注意到您的浏览器将始终加载多个文件,但同时下载的最大数量因浏览器和正在使用的版本而异。

If your questions are related to the title ( How can I reduce the load time of my javascripts? ) a good approach is to 如果你的问题与标题有关( 如何减少我的javascripts的加载时间? ),一个好的方法是

  • review the code, removing and optimizing where necessary 检查代码,删除并在必要时进行优化
  • concatenate all files (in the order of loading) 连接所有文件(按加载顺序)
  • minify the code 缩小代码
  • serve the single file with gzip compression 使用gzip压缩服务单个文件
  • make use of caching to avoid unnecessary requests 利用缓存来避免不必要的请求

Depending on the browser, different amounts of files will be loaded in parallel. 根据浏览器的不同,将并行加载不同数量的文件。 They should be executed in parallel, but for the best results attach an event handler to the page like onload . 它们应该并行执行,但为了获得最佳结果,请将事件处理程序附加到页面,如onload

Concatenating your files will improve load times if they don't change by reducing the server round trip. 如果通过减少服务器往返而不更改文件,则连接文件将会缩短加载时间。 You can also try using a cache manifest or loading tertiary scripts lazily with AJAX. 您还可以尝试使用缓存清单或使用AJAX懒惰地加载三级脚本。

You can try with this techniques, whichever is suitable with your existing code... 您可以尝试使用这种技术,以适合您现有代码的方式...

1. Use minify version of JS code
2. Add only one external JS file (copy all content to one)
3. Use cookie less domain

您可以在将文件推送到生产时合并文件以获取单个文件[并最小化它们] ...这样您将保存请求手动检查以及介于两者之间的所有步骤...

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

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