Note that there are some explanatory texts on larger screens.

plurals
  1. PO$.ready() before closing body
    primarykey
    data
    text
    <p>This is not a real coding question, more of a real-world statement.</p> <p>I have previously <a href="https://stackoverflow.com/questions/9557846/why-is-jquery-ready-recommended-when-its-so-slow">noted</a> that <code>DOMReady</code> events are slow, very slow. So, I noticed while browsing the jQuery source that the jQuery domeready event can be trigger using <code>$.ready()</code>. Then I thought, placing this simple execution script just before closing the body should trigger all the "onDomReady" listeners that where previoulsy attached. And yes, it works as expected:</p> <pre><code> &lt;script&gt;$.ready()&lt;/script&gt; &lt;/body&gt; </code></pre> <p>Here are two examples, this one measures the ms spent while waiting for DOMReady:</p> <p><a href="http://jsbin.com/aqifon/10" rel="nofollow noreferrer">http://jsbin.com/aqifon/10</a></p> <p>As you can see, the DOMReady trigger is very natively slow, the user has to wait for a whole 200-300 milliseconds before the domready script kick in.</p> <p>Anyway, if we place <code>$.ready()</code> just before closing the <code>BODY</code> tag we get this:</p> <p><a href="http://jsbin.com/aqifon/16" rel="nofollow noreferrer">http://jsbin.com/aqifon/16</a></p> <p>See the difference? By triggering domready manually, we can cut off 100-300 ms of execution delay. This is a major deal, because we can rely on jQuery to take care of DOM manipulations before we see them.</p> <p>Now, to a question, I have never seen this being recommended or discussed before, but still it seems like a major performance issue. Everything is about optimizing the code itself, which is good of course, but it is in vain if the execution is delayed for such a long time that the user sees a "flash of "unjQueryedContent".</p> <p>Any ideas why this is not discussed/recommended more frequently?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload