Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good ways to improve jQuery selector performance?

I'm looking for any way that I can improve the selector performance of a jQuery call. Specifically things like this:

Is $("div.myclass") faster than $(".myclass")

I would think it might be, but I don't know if jQuery is smart enough to limit the search by tag name first, etc. Anyone have any ideas for how to formulate a jQuery selector string for best performance?

like image 532
JC Grubbs Avatar asked Sep 05 '08 16:09

JC Grubbs


People also ask

Which jQuery selector is fastest?

ID and Element selector are the fastest selectors in jQuery.

Which selector is faster in JavaScript?

Ofceauce ID is a faster selector in both CSS and JavaScript.


Video Answer


1 Answers

There is no doubt that filtering by tag name first is much faster than filtering by classname.

This will be the case until all browsers implement getElementsByClassName natively, as is the case with getElementsByTagName.

like image 187
kamens Avatar answered Sep 28 '22 03:09

kamens