来自国外的页面JavaScript文件优化 |
||||||||||||
本文标签:JavaScript,文件优化 The problem: scripts block downloadsLets first take a look at what the problem is with the script downloads. The thing is that before fully downloading and parsing a script, the browser cant tell whats in it. It may contain Heres how the timeline looks like when downloading a slow JavaScript file (exaggerated to take 1 second). The script download (the third row in the image) blocks the two-by-two parallel downloads of the images that follow after the script: ![]() Heres the example to test yourself. Problem 2: number of downloads per hostnameAnother thing to note in the timeline above is how the images following the script are downloaded two-by-two. This is because of the restriction of how many components can be downloaded in parallel. In IE <= 7 and Firefox 2, its two components at a time (following the HTTP 1.1 specs), but both IE8 and FF3 increase the default to 6. You can work around this limitation by using multiple domains to host your components, because the restriction is two components per hostname. For more information of this topic check the article “Maximizing Parallel Downloads in the Carpool Lane” by Tenni Theurer. The important thing to note is that JavaScripts block downloads across all hostnames. In fact, in the example timeline above, the script is hosted on a different domain than the images, but it still blocks them. Scripts at the bottom to improve user experienceAs Yahoo!s Performance rules advise, you should put the scripts at the bottom of the page, towards the closing Non-blocking scriptsIt turns out that there is an easy solution to the download blocking problem: include scripts dynamically via DOM methods. How do you do that? Simply create a new var js = document.createElement(script); Heres the same test from above, modified to use the script node technique. Note that the third row in the image takes just as long to download, but the other resources on the page are loading simultaneously:
Test example As you can see the script file no longer blocks the downloads and the browser starts fetching the other components in parallel. And the overall response time is cut in half. DependenciesA problem with including scripts dynamically would be satisfying the dependencies. Imagine youre downloading 3 scripts and Well, the simplest thing is to have only one file, this way not only avoiding the problem, but also improving performance by minimizing the number of HTTP requests (performance rule #1). If you do need several files though, you can attach a listener to the scripts Using YUI Get utilityThe YUI Get Utility makes it easy for you to use script includes. For example if you want to load 3 files, var urls = [one.js, two.js, three.js]; YUI Get also helps you with satisfying dependencies, by loading the scripts in order and also by letting you pass an var myHandler = { Again, note that YUI Get will request the scripts in sequence, one after the other. This way you dont download all the scripts in parallel, but still, the good part is that the scripts are not blocking the rest of the images and the other components on the page. Heres a good example and tutorial on using YUI Get to load scripts. YUI Get can also include stylesheets dynamically through the method Which brings us to the next question: And what about stylesheets?Stylesheets dont block downloads in IE, but they do in Firefox. Applying the same technique of dynamic inserts solves the problem. You can create dynamic link tags like this: var h = document.getElementsByTagName(head)[0]; This will improve the loading time in Firefox significantly, while not affecting the loading time in IE. Another positive side effect of the dynamic stylesheets (in FF) is that it helps with the progressive rendering. Usually both browsers will wait and show blank screen until the very last piece of stylesheet information is downloaded, and only then theyll start rendering. This behavior saves them the potential work of re-rendering when new stylesheet rules come down the wire. With dynamic But before you go ahead and implement dynamic Other ways?There are other ways to achieve the non-blocking scripts behavior, but they all have their drawbacks.
FutureSafari and IE8 are already changing the way scripts are getting loaded. Their idea is to download the scripts in parallel, but execute them in the sequence theyre found on the page. Its likely that one day this blocking problem will become negligible, because only a few users will be using IE7 or lower and FF3 or lower. Until then, a dynamic script tag is an easy way around the problem. Summary
|