Reduce Your jQuery Script Loading Times

Share this article

the-only-script-in-your-head

Hi guys, I’ve stumbled upon a jQuery script that reduces the loading time of your JavaScript include files. It works by loading all your dot js files at the same time inside an array. I’ve tried the tests out and it does seem to load much quicker than loading your files in the standard way. See the proof below and try it for yourself!

The Standard Way

<!-- 5 singlar js includes -->
<script src="https://github.com/DmitryBaranovskiy/raphael/raw/master/raphael.js"></script>
<script src="https://github.com/jquery/jquery-ui/raw/master/jquery-1.4.4.js"></script>
<script src="https://github.com/smith/scripty2/raw/master/lib/prototype.js"></script>
<script src="https://github.com/headjs/www/raw/master/content/test/jquery-ui.js"></script>
<script src="https://github.com/kosmas58/compass-jquery-plugin/raw/master/lib/jslint.js"></script>

script-in-head

The head.js Way

<!-- 1 js include (which calls the others async) -->
<script src="../media/js/head.min.js"></script> 
<script>
head.js("https://github.com/DmitryBaranovskiy/raphael/raw/master/raphael.js")
	 .js("https://github.com/jquery/jquery-ui/raw/master/jquery-1.4.4.js")
	 .js("https://github.com/smith/scripty2/raw/master/lib/prototype.js")
	 .js("https://github.com/headjs/www/raw/master/content/test/jquery-ui.js")
	 .js("https://github.com/kosmas58/compass-jquery-plugin/raw/master/lib/jslint.js");
</script>

head-js-demo

Document Ready with head.js

Usually with jQuery code you need to wait until the DOM is ready. This is the same when using head.js and this is how you can acheive the same result.

//same as document.ready() function
head.ready(function() {
     //code to be executed once .js files have loaded.
});

Demo
Download

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week