简体   繁体   English

如何减少页面加载时间?

[英]How to reduce the page load time?

I have a web page I run locally on a WebKit mobile browser (not on a web server) which has around 27 MB of JavaScript files, YES 27 MB of JavaScript files. 我有一个网页,我在WebKit移动浏览器(不是在Web服务器上)本地运行,它有大约27 MB的JavaScript文件,是27 MB的JavaScript文件。 It is because I have custom JSON objects and arrays hard-coded inside my .js file. 这是因为我在.js文件中有自定义JSON对象和硬编码的数组。

I have split the complete JS contain into 27 small .js files of around 1 MB. 我已将完整的JS包含分成27个大约1 MB的小.js文件。

The problem is that when I includes these .js files inside my header, the page load time increases very much. 问题是,当我在头文件中包含这些.js文件时,页面加载时间会增加很多。

I'd like to know how can I reduce the page load time in such a case where the js files are required. 我想知道如何在需要js文件的情况下减少页面加载时间。

1) Is there a way wherein we can inject the .js files inside the HTML after the page loads for the first time? 1)有没有办法在第一次加载页面后我们可以在HTML中注入.js文件? (because the JavaScript content comes into picture only after a link is clicked on the page) (因为只有在页面上点击链接后,JavaScript内容才会显示在图片中)

2) What would be an optimium solution to includes such a large JavaScript content inside a web page? 2)在网页中包含如此大的JavaScript内容的优化解决方案是什么? I have minified my all js files to reduce the file size as much as possible! 我已经缩小了我的所有js文件以尽可能减少文件大小!

Thanks in advance. 提前致谢。

UPDATE 1: 更新1:

The page runs locally and no WEB SERVER is involved. 页面在本地运行,不涉及WEB SERVER。 Finally, it would run inside a mobile browser, and so that's how all the problem arised ie the load timing is very high in mobile browser, so want to reduce the initial load time. 最后,它将在移动浏览器中运行,因此所有问题都出现了,即移动浏览器中的加载时间非常高,因此希望减少初始加载时间。

(Note: Most of the below was written before you'd bothered to tell us you were running an HTML file locally in a mobile browser without using a web server. Much of it still applies, some of it doesn't, but I've left it for others actually doing web pages.) (注意:下面的大部分内容都是在您打算告诉我们您在移动浏览器中本地运行HTML文件而不使用Web服务器之前编写的。其中大部分仍然适用,其中一些不适用,但我'我把它留给其他人实际做网页。)

1) Is there a way wherein we can inject the .js files inside the HTML after the page loads for the first time? 1)有没有办法在第一次加载页面后我们可以在HTML中注入.js文件?

Yes, it's actually really easy (live example: run / edit ): 是的,它实际上非常简单(实例: 运行 / 编辑 ):

var script = document.createElement('script');
script.src = "path/to/the/file.js";
document.body.appendChild(script);

Note that the script will load asynchronously (you can't just assume it's loaded after the appendChild call). 请注意,脚本将异步加载(您不能假设它在appendChild调用之后加载)。

But if your goal is just to have the page showing while the 27MB file downloads, you can just put your script tag at the end of your page, just before the closing </body> tag. 但是,如果您的目标只是在下载27MB文件时显示页面,则可以将script标记放在页面末尾,就在结束</body>标记之前。 Update : If you're running a local HTML file, not a web page, I'd think this is all you'd need: A single script tag at the end of the page loading your 27MB .js file. 更新 :如果您正在运行本地HTML文件而不是网页,我认为这就是您所需要的:在页面末尾加载27MB .js文件的单个脚本标记。

2) What would be an optimium solution to includes such a large JavaScript content inside a web page? 2)在网页中包含如此大的JavaScript内容的优化解决方案是什么?

Ideally, reduce the size if at all possible. 理想情况下,尽可能减小尺寸。 If you can demand-load assets as you need them (either using the technique above or ajax ), do that instead. 如果您可以根据需要加载资产(使用上述技术或ajax ),请改为使用。 Update : If you're running a local file, not a web page, you basically can't do ajax reliably. 更新 :如果您运行的是本地文件,而不是网页,则基本上无法可靠地执行ajax。 But you can demand-load what you need, when you need it, via adding script elements as per the above. 但是,您可以根据需要通过按上述方式添加script元素来根据需要加载所需内容。

Regarding your 27 1MB .js files: If you hardcode the script tags for them, it's much better to do one 27MB file than 27 1MB files. 关于你的27个1MB .js文件:如果你为它们硬编码script标签,那么做一个27MB文件比27个1MB文件好得多。 Minimizing HTTP requests to your server (ideally at most one .js file and one .css file) is one of the key ways to improve page load time. 最小化对服务器的HTTP请求(理想情况下, 最多只有一个.js文件和一个.css文件)是提高页面加载时间的关键方法之一。 In your case, though, you've said various parts aren't needed until various things are clicked, so you'll probably end up with a main file (which will hopefully be a lot smaller than 27MB), and then a bunch of other things you demand load (as per the above) as necessary. 在你的情况,不过,你说是不是所需要的各种零部件,直到各种各样的东西被点击,所以你可能有一个主文件(这将有望超过27MB的小了很多 ),然后一堆结束你需要加载的其他东西(如上所述)。

Other things you can do: 你可以做的其他事情:

  • Minify , compress , or "compile" your .js files (this means you'll have separate "source" and "production" files, since typically this is a one-way process that removes comments and such) 缩小压缩“编译”您的.js文件(这意味着您将拥有单独的“源”和“生产”文件,因为通常这是一个单向过程,可以删除注释等)
  • Ensure that your server is serving .js files with gzip compression (for instance, with Apache you'd use mod_deflate ); 确保您的服务器使用gzip压缩服务.js文件(例如,使用Apache,您使用mod_deflate ); you can test that it's working with various tools 你可以测试它是否正在使用各种 工具

Also very much worth a read: Best Practices for Speeding Up your Website , which makes the points above and a whole bunch more. 同样非常值得一读: 加速你的网站的最佳实践 ,这使得上面的要点和更多。

You have to combine again that *.js files into one. 您必须再次将* .js文件合并为一个。 That will reduce the server requests that cost in time ! 这将减少及时花费的服务器请求!

Compress your JavaScript content with that tool : http://www.refresh-sf.com/yui/ or that http://closure-compiler.appspot.com/home 使用该工具压缩您的JavaScript内容: http//www.refresh-sf.com/yui/http://closure-compiler.appspot.com/home

Also you have to put that files at the page footer, in order to allow the page to be rendered while you download the js files into the client browser. 此外,您必须将该文件放在页脚,以便在将js文件下载到客户端浏览器时呈现页面。

Another thing that can help is the long time caching of the file. 另一件可以帮助的事情是文件的长时间缓存。 This will allow your JavaScript to be "saved" into client web browser cache and next time is not important to re-downloaded. 这将允许您将JavaScript“保存”到客户端Web浏览器缓存中,下次重新下载并不重要。

Finally I am not 100% sure is that help but try lazy JavaScript loading. 最后我不是100%肯定是帮助,但尝试懒惰的JavaScript加载。


Edit for Lazy Laod 为Lazy Laod编辑

<script type="text/javascript">

    (
        function()
        {
            var sc = document.createElement('script');
            sc.type = 'text/javascript';
            sc.async = true;
            sc.src = 'http://www.url-to-your-javascript.file/my-javascript.js';
            var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(sc,s);
        }
    )();

</script>

Another helpful source 另一个有用的来源

http://gtmetrix.com/dashboard.html http://gtmetrix.com/dashboard.html

Tests your web site speed. 测试您的网站速度。 This will help you find your way on speed optimization about your web site :) 这将帮助您找到有关您网站的速度优化的方法:)

At 27MB it's always going to be slow as you're going to run into the memory limits on the device. 在27MB的情况下,由于您将遇到设备的内存限制,因此总是会很慢。

Most mobiles don't have a lot of RAM and once you load and parse the JSON it's going to be using more the 27MB 大多数手机都没有大量的内存,一旦你加载并解析JSON,它就会使用更多的27MB

Minification will help you but gzip won't as the browser still has to decompress it) 缩小将帮助你,但gzip将不会,因为浏览器仍然必须解压缩它)

If you're just rendering HTML in response to user actions, why don't you create HTML fragments instead of JSON and fetch these and insert them into the DOM when someone clicks on the link? 如果您只是为了响应用户操作而呈现HTML,那么为什么不创建HTML片段而不是JSON并获取这些片段并在有人点击链接时将它们插入到DOM中?

我终于通过为移动设备创建本机应用程序而不是使用混合(HTML5)技术解决了我的这个问题,即我将包含实际应用程序数据的27 MB JS文件移动到sqlite文件并直接在我的Android应用。

I would load the data after page load with ajax. 我会在页面加载后用ajax加载数据。 That is after you loaded the page, you make an asyncronous request for the 27MB of data. 这是在您加载页面后,您对27MB数据发出异步请求。 This allows you also to eventually add a load animation while the data is transferred. 这允许您最终在传输数据时添加加载动画。 You can have a look at jquery to implement this. 您可以查看jquery来实现它。

As a best practice, you should always load javascript in bottom oh html file. 作为最佳实践,您应该始终在底部的html文件中加载javascript。 Put css at top, and js at bottom will greatly help. 将css置于顶部,底部的js将极大地帮助。

27MB is too large. 27MB太大了。 Why are you using hard code in js. 你为什么在js中使用硬编码。 you can use ajax. 你可以使用ajax。 Take help from an expert, may be he can minimize your js 从专家那里获得帮助,可能是他可以最小化你的js

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

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