Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare jquery selectors performance

Looking at improving the performance of my jquery selectors. so any tips or articles as the best per formant jquery selectors? For example selecting the a div's id. Anywhere online I can provide html and compare the different selectors I can use to select the required element.

like image 872
amateur Avatar asked Oct 28 '10 23:10

amateur


People also ask

Which jQuery selector is fastest?

HTML. After seeing the result of the above code, it is crystal clear that the ID selector is the fastest.

Which is faster ID selector or class selector?

As you said, the ID is the faster lookup, but which one you chose has more to do with what you want to do. Think of a class like a category, or classification for different items that have a common or shared aspect to them, while an ID in contrast, has some unique property it is one of a kind.


1 Answers

You can compare selector performance here: http://jsperf.com/

Just setup your HTML, include jQuery and place each selector you want to compare as a test case.

Many of the rules here still apply, however the game changed a bit in jQuery 1.4.3+, after that Sizzle (jQuery's selector engine) will use querySelectorAll() in browsers that support it.

like image 127
Nick Craver Avatar answered Sep 19 '22 03:09

Nick Craver