: What's the best way to load Javascript into a page to optimize performance? Is there a way to load my JavaScript into a page that will make it load faster?
Is there a way to load my JavaScript into a page that will make it load faster?
More posts by @Si4351233
5 Comments
Sorted by latest first Latest Oldest Best
To give an update to this question. I think that in modern, the way of non-blocking loading is not needed anymore, the browser will do it for you.
I have added a question to StackOverflow, I will add the content here aswel.
The only difference is that the load event will be fired a little earlier, but the loading of the files themselves remain the same. I also want to add that even if the onload even fires earlier with the non-blocking script, this does not mean the JS files are fired earlier. In my case the normal setup came out best
Now first the scripts, they look like this:
<script>
(function () {
var styles = JSON.parse(myObject.styles);
for( name in styles ){
var link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('type', 'text/css');
link.setAttribute('href', styles[name]);
document.getElementsByTagName('head')[0].appendChild(link);
}
var scripts = JSON.parse(myObject.scripts);
for( name in scripts ){
var e = document.createElement('script');
e.src = scripts[name];
e.async = true;
document.getElementsByTagName('head')[0].appendChild(e);
}
}());
</script>
myObject.styles is here just an object that holds all the urls for all the files.
I have run 3 test, here are the results:
Normal setup
This is just the normal setup, we have 4 css files in the head, and 3 css files at the bottom of the page.
Now I do not see anything blocking. What I see it that everything is loading at the same time.
Non-blocking JS
Now to take this a little further, I have made ONLY the js files non-blocking. This with the script above. I suddenly see that my css files are blocking up the load. This is strange, because it is not blocking anything in the first example. Why is css suddenly blocking the load ?
Everything non-blocking
Finally I did a test where all the external files are loaded in a non-blocking way. Now I do not see any difference with our first method. They just both look the same.
Conclusion
My conclusion is that the files are already loaded in a non-blocking way, I do not see a need to add special script.
Or am I missing something here?
More:
Article: www.yuiblog.com/blog/2008/07/22/non-blocking-scripts/ Question: stackoverflow.com/questions/6828271/javascript-non-blocking-scripts-why-dont-simply-put-all-scripts-before-body Question: stackoverflow.com/questions/5765113/do-modern-browsers-still-limit-parallel-downloads Code: calendar.perfplanet.com/2010/the-truth-about-non-blocking-javascript/ Code: blog.fedecarg.com/2011/07/12/javascript-asynchronous-script-loading-and-lazy-loading/
You might want to look at the way Google loads Analytics as well:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
As it is considered a "best practice" sort of script: www.stevesouders.com/blog/2009/12/01/google-analytics-goes-async/
Besides Minifing, gziping and CDNing (new word?).
You should consider defer loading. Basically what this does is add the scripts dynamically and prevent blocking, allowing parallel downloads.
There are many ways of doing it, this is the one I prefer
<script type="text/javascript">
function AttachScript(src) {
window._sf_endpt=(new Date()).getTime();
var script = document.createElement("script");
document.getElementsByTagName("body")[0].appendChild(script);
script.src = src;
}
AttachScript("/js/scripts.js");
AttachScript("http://www.google-analytics.com/ga.js");
</script>
Place this just before the closing body tag and use AttachScript to load every js file.
Some more info here www.stevesouders.com/blog/2009/04/27/loading-scripts-without-blocking/
A couple of Google folks announced an new open source project at Velocity 2010 called Diffable. Diffable performs some magic to incrementally publish only the portions of JS, HTML, and CSS which have changed since the version which is stored in the user's cache, rather than sending an entire new file when a new version is released.
This technique is insanely cool, and is currently most effective (and worth the effort) on websites where you are using a large JavaScript code base with small frequent code changes. This applies especially well to applications like Google Maps, which undergoes at least one release every Tuesday, and averages about 100 new releases a year. It also makes a lot of sense in general once HTML5 local storage becomes more widespread.
BTW, if you haven't seen Google's Michael Jones talk about change (in a geospatial context) it's worth watching his entire keynote at GeoWeb 2009.
There are a few things you can do:
Load the HTML and CSS before the javascript. This gives the browser everything it needs to lay out the page and render it. This gives the user the impression that the page is snappy. Place script tags or blocks as close to the closing body tag as possible.
Consider using a CDN. If you are using any of the popular libraries like JQuery many companies (e.g. google, yahoo) operate free CDNs that you can use to load the libraries.
Load code fron an external file instead of an embedded script. This gives the browser the opportunity to cache the JS content and not have to load it at all. Successive page loads will be faster.
Turn on zip compression on the web server.
Yahoo has a great page of suggestions that can help reduce page load times.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.