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 6075453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:29:47+00:00 2026-05-23T10:29:47+00:00

Basically, is $(#someid) or $(.someclass) faster than $([someattr=’value’]) I would imagine that it is

  • 0

Basically, is

$("#someid")

or

$(".someclass")

faster than

$("[someattr='value']")

I would imagine that it is (that is, selection by id is fastest, then class, then attribute), but does anyone know for sure?

  • 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-23T10:29:47+00:00Added an answer on May 23, 2026 at 10:29 am

    ID is absolutely the fastest. Part of the reason is that ID is supposed to be unique, so the API stops searching after the ID is found in the DOM.

    If you must use a class or attribute selector, you can improve performance by specifying the optional context parameter.

    For example…

    $(".someclass", "#somecontainer")
    

    Where somecontainer is something like a div, surrounding an element with class someclass. This can offer a huge performance benefit in the cases where somecontainer comprises a small fraction of the DOM.


    UPDATE:

    I did some tests a couple years ago around the context parameter. After reading the comments below I was curious if anything has changed. Indeed, it seems the scene has changed somewhat with today’s browsers. Maybe it also has to do with improvements in jQuery? I don’t know.

    Here are my results with 10,000 iterations (code is below):

    IE9

    $(".someclass") – 2793 ms

    $(".someclass", "#somecontainer") – 1481 ms

    Chrome 12

    $(".someclass") – 75 ms

    $(".someclass", "#somecontainer") – 104 ms

    Firefox 3.6

    $(".someclass") – 308 ms

    $(".someclass", "#somecontainer") – 357 ms

    So among these big 3 modern browsers, the context parameter only seems to help IE9. Older browsers will also benefit from the context parameter. But considering the prevalence of each of these browsers and averaging everything out, the net gain is somewhat of a wash now.

    And here’s the code in case anyone wants to try it themselves…

    <html>
        <head>
            <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
            <script type="text/javascript">
                $(document).ready(function(){
    
                    startTime = new Date().getTime();               
                    for (i = 0; i < 10000; i++)
                    {
                        s = $(".someclass");
                    }           
                    $("#withoutcontext").html(elapsedMilliseconds(startTime));
    
    
                    startTime = new Date().getTime();
                    for (i = 0; i < 10000; i++)
                    {
                        s = $(".someclass", "#somecontainer");
                    }           
                    $("#withcontext").html(elapsedMilliseconds(startTime));
    
                });
    
                function elapsedMilliseconds(startTime)
                {
                    var n = new Date();
                    var s = n.getTime();
                    var diff = s - startTime;
                    return diff;
                }
            </script>
        </head>
        <body>
            <h1>jQuery Selector Performance: Context vs No Context</h1>
    
            <h2>$(".someclass")</h2>
            <span id="withoutcontext">---</span> ms<br /><br />
    
            <h2>$(".someclass", "#somecontainer")</h2>
            <span id="withcontext">---</span> ms<br /><br />
    
            <hr />
    
            <p class="a">a</p>
            <p class="b">b</p>
            <p class="c">c</p>
            <p class="a">a</p>
            <p class="b">b</p>
            <p class="c">c</p>
            <p class="a">a</p>
            <p class="b">b</p>
            <p class="c">c</p>
            <p class="a">a</p>
            <p class="b">b</p>
            <p class="c">c</p>
            <p class="a">a</p>
            <p class="b">b</p>
            <p class="c">c</p>
            <div id="somecontainer">
                <p class="a">a</p>
                <p class="b">b</p>
                <p class="c">c</p>
                <p class="someclass">someclass</p>
            </div>
            <p class="a">a</p>
            <p class="b">b</p>
            <p class="c">c</p>
            <p class="a">a</p>
            <p class="b">b</p>
            <p class="c">c</p>
            <p class="a">a</p>
            <p class="b">b</p>
            <p class="c">c</p>
            <p class="a">a</p>
            <p class="b">b</p>
            <p class="c">c</p>
            <p class="a">a</p>
            <p class="b">b</p>
            <p class="c">c</p>
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically this function is meant to store the height value of the element that
Basically, something better than this: <input type=file name=myfile size=50> First of all, the browse
Basically, I would like a brief explanation of how I can access a SQL
Basically I’ve heard that certain conditions will cause .NET to blow past the finally
Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer
I have a big fat query that's written dynamically to integrate some data. Basically
Basically from a database I am getting data that is formatted like this nameofproject101
I have something that looks like the following document structure: public class Document {
Basically, I have a main page (parent page) with a link that opens up
basically I have a function that resizes elements accordingly with jquery triggering the function

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.