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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:29:29+00:00 2026-05-27T18:29:29+00:00

Scroll down for the getById.getByClassName vs. qSA comparison! If we wanted to select all

  • 0

Scroll down for the getById.getByClassName vs. qSA comparison!


If we wanted to select all elements of class "bar" which are inside the element with the ID "foo", we could write this:

$( '#foo .bar' )

or this:

$( '.bar', '#foo' )

There are of course other methods to achieve this, but for the sake of this question, let’s compare only these two methods.

So, which of the above methods performs better? (Which needs less time to execute?)

I have written this performance test:

(function() {
    var i;
    
    console.time('test1');
    for( i = 0; i < 100; i++ ) {
        $('#question-mini-list .tags');
    }
    console.timeEnd('test1');
    
    console.time('test2');
    for( i = 0; i < 100; i++ ) {
        $('.tags', '#question-mini-list');
    }
    console.timeEnd('test2');
})();

You have to execute it from within the console on the Stack Overflow start-page. My results are:

Firefox:
test1: ~90ms
test2: ~18ms

Chrome:
test1: ~65ms
test2: ~30ms

Opera:
test1: ~50ms
test2: ~100ms

So in Firefox and Chrome, the second method is multiple times faster – just as I expected. However, in Opera the situation is reversed. I wonder what’s going on here.

Could you please run the test on your machine and explain why Opera performs differently?


Update

I’ve written this test, in order to investigate whether Opera’s qSA really is super-fast. As it turns out, it is.

(function() {
    var i, limit = 5000, test1 = 'test1', test2 = 'test2';

    console.time( test1 );
    for( i = 0; i < limit; i += 1 ) {
        document.getElementById( 'question-mini-list' ).getElementsByClassName( 'tags' );
    }
    console.timeEnd( test1 );

    console.time( test2 );
    for( i = 0; i < limit; i += 1 ) {
        document.querySelectorAll( '#question-mini-list .tags' );
    }
    console.timeEnd( test2 );
})();

Again, you have to run this code from within the console on the Stack Overflow start-page. I used the Firebug Lite bookmarklet for IE9 (since that browser doesn’t implement console.time).

So, I compared this method:

document.getelementById( 'A' ).getElementsByClassName( 'B' );

to this method:

document.querySelectorAll( '#A .B' );

I’ve executed the above script five consecutive times in each browser. The arithmetic means are:

enter image description here

(All numbers are in milliseconds.)

So, the performance of the first method is pretty much the same in the tested browsers (16-36ms). However, while qSA is much slower compared to the first method, in Opera it actually is faster!

So, qSA optimization is possible, I wonder what the other browsers are waiting for…

  • 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-27T18:29:29+00:00Added an answer on May 27, 2026 at 6:29 pm

    jQuery/Sizzle will avoid using the JavaScript based Sizzle engine if the browser supports querySelectorAll, and if you pass a valid selector (no custom, non-CSS selectors).

    This means that you’re ultimately comparing implementations of querySelectorAll, assuming you’re testing browsers that support it.

    There are other optimizations that jQuery or Sizzle uses, so it’s tricky when comparing different types of DOM selection in different browsers.

    The reason for Opera’s performance result seems to be that they have a very highly optimized querySelectorAll implementation. qSA, being a relatively new method, hasn’t been quite as optimized in some browsers compared to older methods like getElementsByTagName.

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

Sidebar

Related Questions

I have a listbox inside an update panel. When I scroll down and select
(Scroll down to bottom of post to find solution.) Got a asp.net page which
What I want to do is scroll down the window when I expand elements
I saw this the other day (scroll all the way down to see some
To scroll down my chat DIV I am using the following code which is
UPDATED : Scroll down for updated. I have a UITableView which cells contain few
Say I have some grid that you need to scroll down to see all
I have a sort of sidebar on my website, which has to scroll down
I have a listview which contains N no. of items. When user scroll down
I'm opening Outlookelements with the shown Code (scroll down; C# Code). Sometimes the elements

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.