Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6748615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:35:11+00:00 2026-05-26T12:35:11+00:00

I noticed that some of developers in our team are creating long jQuery selectors

  • 0

I noticed that some of developers in our team are creating long jQuery selectors like:

'div.someclass > span.someotherclass ...'

I know, that jQuery will try to use native DOM methods for classes and tags, but I think if it would be more efficient to use something lite this:

'[data-gid="my-element-group"]'

instead of those long mixed class/tag selectors.

Will custom attribute selectors be faster than long class/tag selectors or it depends on circumstances? Which approach to choose for a Javascript-heavy website?

P.S. I guess, those long selectors are also dangerous if HTML designers and Javascript coders are working separately – designer may break Javascript functionality just by changing CSS classes or moving tags around. But if they see ids and data-gids, they can leave them alone, and that should be much safer… but will it sacrifice performance?

P.P.S. I don’t care about invalid HTML4 because of custom attributes, the performance is much more important for most of my clients.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:35:12+00:00Added an answer on May 26, 2026 at 12:35 pm

    The longer selectors should be faster. Here’s an example. Given the following HTML:

    <div class="testDiv">
        <span class="testSpan"></span>
    </div>
    <div>
        <span data-group="testSpan"></span>
    </div>
    

    And the following jQuery:

    var elems1 = $("div.testDiv > span.testSpan");
    var elems2 = $("[data-group='testSpan']");
    

    Here’s the results from a quick test:

    enter image description here

    However, it’s worth noting that in the real world the difference is going to be minimal and you should use whichever suits you best.

    You could make your data-* selector slightly better (performance-wise) by making it more specific:

    var elems = $("span[data-group='testSpan']");
    

    But another quick test reveals (for me, in Chrome 15 at least) that the longer selectors still win.

    I think in older browsers with no support for querySelectorAll, the difference will be even more pronounced in favour of the longer selectors, which can use the native getElementsByTagName.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed that some stylesheets have something like this: body { font-size: 62.5%/1.2em; }
I have noticed that some apps like Safari and Mail show a loading indicator
I've playing around with linux and noticed that for some mysterious reason commands like
I recently noticed that some (not all) of my javascript and jQuery scripts wouldn't
I've noticed that some web developers put IDs on scripts tags. For example: <script
I noticed that some enumerations have None as a enumeration member. For example what
I've noticed that some sites (usually banks) suppress the ability to paste text into
While reading cocoa tutorials i've noticed that some of the tutorials use AppDelegate and
I've noticed that on some websites (including SO) the link to the CSS will
Quick question, I noticed that on some of my header directors I was getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.