1

In this article: https://www.html5rocks.com/en/tutorials/speed/script-loading/

They are saying:

Scripts that are dynamically created and added to the document are async by default, they don’t block rendering

But the "execution of javascript" is always blocking rendering, so how can they say they don't block rendering?

I can make it more clear with this example:

SCRIPT.JS

// Synchronous delay of 5 seconds
var timeWhile = new Date().getTime(); 
while( new Date().getTime() - timeWhile < 5000 );

INDEX.HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Test</title>
    <script>
        ['script.js'].forEach( function( src ) {
            var script = document.createElement( 'script' );
            script.src = src;
            script.async = true;
            document.head.appendChild(script);
        });

        // Some other javascript execution, let's say execution delay of 2 seconds.
        var timeWhile = new Date().getTime(); 
        while( new Date().getTime() - timeWhile < 2000 );
    </script>
</head>
<body>
    Some HTML line and this is above the fold
</body>
</html>

I tested it in Chrome and Firefox and both browsers are showing: "Some HTML line and this is above the fold" after 7 seconds and not earlier. So the execution of script.js is blocking rendering, because otherwise the browser would show something after 2 seconds.

Notice: You can also do the test without the delay of 2 seconds, but then you can get different results when repeating the the test. With the delay of 2 seconds, i am giving the browser some extra time, so i am sure script.js has been downloaded, before finishing rendering. Anyway you can get the same results without that delay, but it's only to make it more clear in this post.

What is happening here: - The browser first will create the element (async script tag with external file script.js) - Then it starts downloading script.js in parallel, so the everything goes further while downloading script.js - Then the browser is executing the javascript delay of 2 seconds. Meanwhile script.js has been downloaded. - Maybe "Some HTML line and this is above the fold" is already in the DOM, but that's not necessary. Anyway it has not been rendered yet, because there is nothing to see yet on the screen. - Script.js has been downloaded, so it starts to execute the javascript. The execution of Javascript will always block rendering, so now "rendering" has to wait for 5 seconds.

So when scripts are dynamically created, script.js will be downloaded in parallel, but that does not necessarily mean the script is not blocking rendering anymore. They could say the (down)load of script.js is not blocking rendering, but they don't say it like that.

Then how they can say something like that? I don't see it only here, but also in other official Google documentation.

Now people can read it and make something like my example (i only made the "execution time" more bigger with delays, to make it more clear). Then people can think: Nice! The javascript is not blocking rendering, but actually it is blocking, so they could better make other choises in terms of page speed.

msanford
  • 11,803
  • 11
  • 66
  • 93
  • They mean it won't stop the page from rendering. The body and the message will load and display without waiting for the javascript files to load. – Ozan Nov 01 '17 at 13:43
  • It only means that the loading if the script is not blocking the rendering of the page. What you do inside the script still might. – Constantin Groß Nov 01 '17 at 13:43
  • 2
    It will block rendering, but after the page has rendered, so its not that noticible – Jonas Wilms Nov 01 '17 at 13:43
  • @Ozan I said: "They could say the (down)load of script.js is not blocking rendering, but they don't say it like that." So it's the same what you mean, i think. But then you can not say that a script won't stop the page from rendering, because the execution does, so then they had to say: down(loading) scripts. Really big difference! – Maarten Bruins Nov 01 '17 at 13:54
  • @Jonasw w That's incorrect, the page has not been rendered yet. Otherwise it would show something within 7 seconds. And @ Connum see my reply to @ Ozan same reason / argument. – Maarten Bruins Nov 01 '17 at 13:57
  • Actually you can say precisely that **it does not stop the page from rendering**. The page is loaded and rendered without being interrupted by those javascript files. – Ozan Nov 01 '17 at 13:59
  • @Ozan You have to explain that more, because if that would be the case then why the browser is not showing any content between 2-7 seconds? The javascript execution of 5 seconds is avoiding that. It blocks rendering. – Maarten Bruins Nov 01 '17 at 14:02

0 Answers0