2

I'm currently writing very explicit selectors in my jQuery code. For example given this markup

<div class="codeblue">
    <div class="codeyellow">
        <div class="codeorange">
            <div class="codewhite">
                <select id="codeChoice">
                    <option>red</option>
                    <option>green</option>
                    <option>black</option>
                </select>
            </div>
        </div>
    </div>
</div>

I use this explicit selector

var $select = $('.codeblue .codeyellow .codeorange .codewhite #codeChoice');

Would it be better to do this instead?

var $codeBlue = $('.codeblue');
var $select = $codeBlue.find('#codeChoice');

Are there any performance hits for not using explicit selectors?

Halcyon
  • 14,631
  • 17
  • 68
  • 99

4 Answers4

13

Actually, since IDs are unique you can simply select on the ID from the start.

var $select = $('#codeChoice');

As far as your other question goes, there is no easy answer. Multiple selectors can cause slowdowns, but you really have to try to know. Furthermore, it depends on the browser. Your best bet for checking it out is using http://jsperf.com/

Also, as is noted in this, you should make sure to place the less specific selector on the left, like this:

var $codeBlue = $('.codeblue #codeChoice');
Community
  • 1
  • 1
Johanna Larsson
  • 10,531
  • 6
  • 39
  • 50
  • Or, if the same script is reused on another page where `#codeChoice` can be inside a different structure: `$('.codeblue #codeChoice')`. – bfavaretto Oct 22 '12 at 17:30
  • I'm writing code with other developers and the goal is to have tags with unique ids. However, to ensure that no accidental use of the same id occurs, I wrap my code in a div and treat the div as the root, hence the need for $codeBlue.find('#codeChoice'); Is there any performance hit when using this approach? I understand that selecting by id would be idea, but I can't take that chance just in case someone else uses the same id. If i were the only person on this project, I would simply use the id selector. – Halcyon Oct 22 '12 at 17:33
  • @bfavaretto At that point it would be invalid HTML, does jQuery optimize when it sees `$("tagname #idval")`, skipping ahead to `document.getElementById()`? Edit: it does not optimize, so it is a safe alternative. – Kevin B Oct 22 '12 at 17:33
  • @Halcyon as I wrote in my answer, the only way to know for sure is testing it. IIRC older versions of IE suffer from some slowdowns on class selectors. You should not notice a huge slowdown though – Johanna Larsson Oct 22 '12 at 17:36
  • @KevinB I don't think it optimizes for that. I was thinking on a scenario where (a) the same id is used differently in two different pages (once on each page), (b) the same script is shared by both pages, and (c) the script should target that id on just one of those pages. – bfavaretto Oct 22 '12 at 17:48
  • @bfavaretto Basically what i was getting at is If jquery DID optimize, it would be selecting the same element (the first one) regardless of what context you gave it. But since jQuery doesn't optimize that, it isn't an issue. – Kevin B Oct 22 '12 at 17:50
2

If you use concrete IDs, jQuery will be faster because it uses the native method document.getElementById(); As your first selector includes 4 Classes ( = Slow Detection ) and 1 id (= Faster Detection) and your second selector 1 Class ( = Slow Detection) and 1 Id ( = Faster Detection) , the second will be faster.

Generally selectors will be faster as less pieces are included.

tobspr
  • 8,200
  • 5
  • 33
  • 46
0
var $select = $("#codeChoice"); 

should be enough, as the id should be unique

st3inn
  • 1,556
  • 9
  • 17
0

The fastest way to select an element in jQuery is by ID. Accessing element by Id is good for performance. As the id is unique on the page.

High performance Javascript book

var $select = $('#codeChoice");
insomiac
  • 5,648
  • 8
  • 45
  • 73