Archive for category jQuery

So you think you’re writing effiecent jQuery selectors?

Think again if you’re writing selectors such as

$("div#myId")

When selecting against the DOM ID the fastest way to get the node is to use only the id:

$("#myId") //will translate into document.getElementById("myId") internally

otherwise jQuery will first get all DIV-nodes then manually iterate over them looking for an attribute id that’s equal to “myId”. In a case with a large DOM-tree this could be a large performance-hit making your site unusable on slower computers if you’re using selectors like this everywhere.

On the other hand when selecting against CSS-classes it’s faster to use

$("div.myClass")

than just

$(".myClass")

,

2 Comments