Yes, I was debugging a problem and it turns out one of my developers was indeed trying to find an non-unique ID within a context.  But it always retrieved the first element with that ID in the document.  It was immediately clear what it was doing.

As for $(".myClass", myContext); being faster than searching by ID, it has to be a large document.  On the order of several hundred nodes.  When searching within a context I have found that it's pretty much constant time, no matter how large the document is.  So even though searching by class is slow, when the document gets very large it starts to outperform searching by ID.  Because, at least in IE, document.getElementById is not a constant lookup.  I don't know how it works; I assumed that it would use a hash table too and therefore be constant time.  But it definitely increases.  It's probably not linear, but it's not strictly constant for sure.

Brendan

On 10/10/06, Christof Donat <[EMAIL PROTECTED]> wrote:
Hi,

> Because if the size of your markup is large then this
>
> $("#myId", myContext);
>
> may actually be slower then this
>
> $(".myClass", myContext);

It depends on what the browser does for getElementById().

If the browser walks through all the elements in your document to find the ID,
then you are right.

If the browser sequentially searches a list of all IDs of the document, you
might be right if most elements in your document have an ID.

If the browser uses a sorted list of IDs, then it can find your element in
logarithmic time (with binary search). To beat that your context needs to be
really small.

If the browser uses sorted hashbuckets it can find the bucket in constant time
and find the element in the bucket in logarithmic time. I guess you won't
beat that.

Christof

_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to