Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One big thing regarding optimization in jQuery is knowing that the jQuery function <code>jQuery()</code> often aliased as <code>$()</code> is indeed a function.</p> <p>That means that using <code>$('#some_id')</code> is actually calling a function which <strong>looks for</strong> the HTML element with the id #some_id. This is costly performance wise because <strong>looking for</strong> something in the DOM means traversing a tree and checking/ comparing the properties of the nodes.</p> <p>You should fetch an HTML element from the DOM (with jQuery) and save the reference into a variable. And later on in the script you'll reference that HTML element using the same variable so as not to traverse the DOM again <strong>looking for</strong> the same element.</p> <p>By using jQuery's <code>method chaining</code> approach (as suggested by @meloncholy) you would obtain the same benefits as defining a variable plus a more compact syntax. This is useful for manipulating an object you don't need a reference to later on, so you just stack-up all the manipulations you need done to it.</p> <p>An example would be:</p> <pre><code>var some_object = $('#some_id'); some_object.hide(); if (some_object.hasClass('some_class')) { ... </code></pre> <p>I can't say that there will be a <strong>noticeable</strong> performance improvement to a script this size. If you had a few hundred lines of jQuery code all abusing the <code>jQuery()</code> function and a few hundred HTML elements in the tree and then you refactored the jQuery code as I indicated then you would have a speedup.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      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