Search code examples
javascriptjquerypagespeed

Is having 100 document ready better or worse than 1 document ready?


Just wondering if the amount of document.ready calls affects page load speed. Is there a way in Gulp / Grunt to uglify / minify JS by removing seperate document ready functions?


Solution

  • Just check it!

    I don't see significant difference in Chrome.
    As I know, it was critical for IE8, but didn't check this fact.
    IE11 shows 2 seconds on the first snippet, when the others take 200 ms only.

    Also, seems like jQuery already aggregates load events.

    Don't forget

    1. When you are running same code in one tab, browser remembers something and runs it faster.
    2. Reload the page is not enought. Open a new tab instead.
    3. After opening a new tab, run snippets in different order.
    4. If the snippet is ran first on the tab, it will get additional slowdown, comparing the other three.

    for (var q=0; q<1000; ++q) {
      document.addEventListener('DOMContentLoaded', (function (i) {
        console.log(i);
      }).bind(null, q));
    }
    
    document.addEventListener('DOMContentLoaded', function () {
      document.querySelector('output').textContent = performance.now().toFixed(3);
    });
    <output></output>

    document.addEventListener('DOMContentLoaded', function () {
      for (var q=0; q<1000; ++q) {
        (function (i) {
          console.log(i)
        }).bind(null, q)();
        
        document.querySelector('output').textContent = performance.now().toFixed(3);
      }
    });
    <output></output>

    for (var q=0; q<1000; ++q) {
      $((function (i) {
        console.log(i);
      }).bind(null, q));
    }
    
    $(function () {
      document.querySelector('output').textContent = performance.now().toFixed(3);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <output></output>

    $(function () {
      for (var q=0; q<1000; ++q) {
        (function (i) {
          console.log(i)
        }).bind(null, q)();
        
        document.querySelector('output').textContent = performance.now().toFixed(3);
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <output></output>

    Maybe it's just me as a JavaScript avoider, but none of the scripts have document.ready inside. If you JS guys talk about document.ready, that's a synonym for addEventListener('DOMContentLoaded')?

    There are two events: DOMContentLoaded and load (window.onload). First of them occures when the body pasring is complete, but some assets are loading still. The second - when the page is completely loaded. First one is nice for running scripts with dom manipulations, but browsers not always had support of it.

    So jQuery uses the first of these two events and classic form of subscription was

    $(document).ready(function () {
      // ...
    });
    

    but after some versions if was simplified to passing function directly into jQuery:

    $(function () {
      // ...
    });
    

    So in vanilla examples I'm using the first of 2 events, and in jQuery examples I'm using the short form of subscription on it. As browsers without support of this event are very old it's correct to assume that jQuery always uses DOMContentLoaded (probably the load way is removed in version 2 - didn't check it, but see no reasons to keep it there).