NeilM wrote:
I have just started to use jQuery in my web development and have a
question concerning optimising element selection.

I find that I create a lot of references to id'd elements:

<img id="myImage" src="todaysimage.gif" width="20" height="20" />

Then, in my JavaScript, I want to obtain a reference to the element.
Now, even though it has an id, I tend to use the full CSS descriptor
as, to me anyway, it makes the code more readable:

$("img#myImage").addClass("border");

My question is, apart from the overhead of some extra characters in
the script (e.g. 'img#'), does this style of specification introduce a
performance overhead internally within jQuery? Would/does jQuery
optimise the element selection process if it just 'sees' a simple id
string starting with a #?

$("#myImage").addClass("border");

Thanks.

Yes, it does slightly. After retrieving the element via document.getElementById, jQuery has to perform an additonal check for the node name. But that is neliglible maybe.


--Klaus

Reply via email to