Improve Performance with the Async and Defer Script Attributes

I went to the HTML5 Developer Conference on Monday, and Ilya Grigorik’s (Software Engineering Manager, Google Analytics at Google) performance lecture was the highlight.

He reminded us to always load CSS before JavaScript. He also strongly encouraged developers to make use of the “defer” and “async” attributes on the script tag to improve the speed of your web page.

Defer

<script defer src=”script.js”></script>

The defer attribute informs the script tag to wait until the entire page is loaded before executing the javascript. If your page is dependent on this script for creating DOM elements during the initial load, do not use this attribute, unless you want those elements to load last.

If you use the defer attribute on several script tags, they will be loaded in they are included.

Async

<script async src=”script.js”></script>

The async attribute should be used when you’d like your script to load and execute as soon as possible, without regard to the page load. This attribute should be used on external javascripts to prevent them from delaying your page elements from loading.

Support

The defer attribute is supported by IE5.5+, FF3.5+, Chrome 8+, Safari 5+, iOS 5+ and Android 3+.

The async attribute is supported by IE10+, FF 3.6+, Chrome 8+, Safari 5+, iOS5+ and Android 3+.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s