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

  • Home
  • SEARCH
  • 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 3721152
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:27:30+00:00 2026-05-19T05:27:30+00:00

I was wondering the other day when typing you selectors in jQuery would it

  • 0

I was wondering the other day when typing you selectors in jQuery would it make a difference in performance if you specify your selectors very specific or very loosely.

E.g

$('$myId .someElement')

Or

$('table#myId > tbody > tr > td > div.someElement');

Would the 2nd option be a lot quicker or would the difference be neglectable, say when doing .each() if you need to find the same element over and over.

  • 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-19T05:27:31+00:00Added an answer on May 19, 2026 at 5:27 am

    I would go for:

    $('.someElement', '#myId')
    

    Since getElementById is the fastest selector operation, you are first filtering your content and then searching for the class. Also the fewer the selectors the faster the selections will be (depends if using descendant or child selectors – check comments!) .
    Test based on @Felix test case.

    Edit: so to enhance this answer. Based on the jQuery documentation:

    By default, selectors perform their
    searches within the DOM starting at
    the document root. However, an
    alternate context can be given for the
    search by using the optional second
    parameter to the $() function.

    So when you provide a second parameter (a context), jQuery will search for that element first and then search within it, so it would be translated from:

    $('.someElement', '#myId')
    

    To:

    $('#myId').find('.someElement')
    

    Now the trick is, trying to find the closest container of your element that actually holds an ID and put it as context, since ID selection is the fastest. Now you would say, why not then using the second already translated one, by I wouldn’t actually care that much since the first is cleaner and it’s not much slower:

                        $('.someElement', '#myId')                  $('#myId').find('.someElement')
    Chrome 8.0.552              36,527                                      37,400
    Firefox 3.6.13              17,632                                      19,155
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering how other developers begin refactoring. What is your first step? How
I am wondering what other approaches you would take to do some simple string
I'm wondering if any other C# developers would find it an improvement to have
I was reading this article the other day and was wondering why there was
The other day I was looking into Zend Server and I was wondering why
I was wondering the other day if StackOverflow had an API I could access
the other day I was wondering why scala.collection.Map defines its unzip method as def
I'm copying data from a table to an other one. I'm wondering what would
the other day I read this article by Alan Storm called Using jQuery and
Well the other day I improved the performance of a particualr piece of code

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.