How does jQuery traverse the DOM?
<p>I was wondering how jQuery traverses the DOM when you use a selector.
Does it look up each "first level" element, and then look inside each one of them? Or does it look at every children of those "first level" elements one by one?</p>

<p>Let me explain what i imagined with some quick examples, given the following selector :</p>

<p><code>$("div p#target")</code></p>

<p>Does it proceed more like :</p>

<pre><code>[1] &lt;div&gt;
    [3] &lt;div&gt;
        [5] &lt;p id="target"&gt;&lt;/p&gt;
        &lt;/div&gt;
    &lt;/div&gt;
[2] &lt;div&gt;
    [4] &lt;div&gt;
            &lt;p&gt;&lt;/p&gt;
        &lt;/div&gt;
    &lt;/div&gt;
</code></pre>

<p>Or like :</p>

<pre><code>[1] &lt;div&gt;
    [2] &lt;div&gt;
        [3] &lt;p id="target"&gt;&lt;/p&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;div&gt;
            &lt;p&gt;&lt;/p&gt;
        &lt;/div&gt;
    &lt;/div&gt;
</code></pre>
