12 Javascript defer or async?
#performancematters #fastweb #jvmtech #jvmperformanceseries

12 Javascript defer or async?

When adding interactivity to our web pages we heavily rely on JavaScript. But adding JavaScript comes with a price. The much feared render block. So let’s examine what render blocking is and why it’s happening. 

Traditionally, scripts were added within the head tag. When the browser’s main thread was parsing the page it evaluated the script tag, it stopped the parsing process, downloaded the JavaScript resource and executed the script. This strategy led to slow renderings, as the parsing process was stopped every time.

No alt text provided for this image

Then developers tried to improve performance by moving the scripts to the very end of the source code, which delivered better results but led to other problems.   

With HTML5 new attributes for script tags arrived to address this problem. Async and defer. Both tackle performance problems in a different way and neither guarantees a non blocking behaviour. This needs to be addressed in the script itself. But you do have more control about what happens when.

When using the async attribute, the JavaScript file gets downloaded asynchronously and then executed as soon as it’s downloaded.

When using defer, the JavaScript file gets also downloaded asynchronously. The key difference is, that it is executed when the document parsing is completed.

So, the advantage here is, that scripts will execute in the same order as they are linked in the HTML which is not guaranteed with async.

This makes defer the attribute of choice when a script depends on each other. Async is fine when you want a script to be executed early in the loading process.

Tomorrow follows: #13 Web Performance Timing APIs

To view or add a comment, sign in

More articles by Thomas Feldhaus

  • The speed of use

    At the previous article "The perception of performance" we found out a lot about the human perception of time and speed…

    1 Comment
  • #19 The perception of performance

    Do milliseconds and kilobytes make the difference? In the previous articles on performance we have heard a lot about…

  • #18 Building a web performance culture.

    Web Performance is not a product feature that can be easily switched on and off, ideally the entire organization…

  • #17 AMP, Performance built-in.

    For 16 episodes we have now bombarded you with tips, tricks and ideas how you can improve the speed of your web app…

  • #16 Webassembly

    WebAssembly or short Wasm is a huge deal as it is enabling deployment of lower level language code on the web for…

  • #15 Image & Video Formats

    In the first years, the Web was a text-based wasteland with only a few image oases available solely through links. This…

    1 Comment
  • #14 PWA/Service Worker

    When thinking about performance, web technologies always had a reputation of being slow and sluggish, especially on…

  • #13 Web Performance Timing APIs

    If you are into web performance you surely stumbled upon very detailed numbers. No matter whether you measure…

  • #11 JavaScript Performance

    Nowadays, web applications are heavily interactive and make use of lots of JavaScript. May they be scripts we wrote for…

  • #10 Resource Hints: Preload, Preconnect, Prerender.

    In the page based web there was for a long time no possibility to actively control the loading process as a developer -…

Explore content categories