4 Performance Tips for Elements Matching in jQuery

/ / JavaScript /
  • 1.6 out of 5 Stars
Please note: this article is over 13 years old. Information may be out of date.
  1. Always use ID to select an element if you can. This is the fastest way to select elements.
  2. Avoid matching elements by class name. Selecting with class name only will make jQuery search every element in the entire page. If you need to match elements by classname then use the element tag combined with the class name.
    Avoid this:
    jQuery(".ElementClass")
    Do this:
    jQuery("div.ElementClass")
  3. Narrow it down. For example, if you know that the elements you want to select are in the top menu of the page then specify it when selecting.
  4. Avoid selecting the same element more than once. Store your selected element or use chaining.
    Avoid this:
    jQuery("#MyElement").addClass("SelectedItem");
    jQuery("#MyElement").show();
    Do this:
    jQuery("#MyElement").addClass("SelectedItem").show();
    Or this:
    var myElement = jQuery("#MyElement");
    myElement.addClass("SelectedItem")
    // some more code...
    myElement.show();
Watch your marketing growth rise to the stars

Rate This Article

How would you rate the quality of this content?
Currently rated: 1.6 out of 5 stars. 3 users have rated this article. Select your rating:
  • 1.6 out of 5 Stars
  • 1
  • 2
  • 3
  • 4
  • 5
Learn how a family bakery used Semrush's digital marketing tools to rank #1 on Google