JQuery class selectors: with or without a tag?

I am trying to find the final answer to the old question: "Should I use the tag name when choosing with the class?"

Wherever I look at jQuery best practices on the Internet, it almost always makes sense to use jQuery ("tag.className"); because it uses getElementByTagName first.

So, I installed the jsPerf test to verify. I used the original markup on my website to test it more in the real world and run the test in all modern browsers, and besides Chrome and Opera all other modern browsers say jQuery (". ClassName"); an order of magnitude faster, although this contradicts almost everything that I read in books and textbooks on the Internet. I guess this is faster because these browsers support getElementByClassName natively.

Here's the benchmark:

http://jsperf.com/length-detection

What do you think is better?

Many thanks

Chris

+3
source share
1 answer

, jQuery . . jsperf: http://jsperf.com/jquery-selecting-by-class

"". , jQuery, , jQuery . getElementsByClassNames, - , jQuery. , : . CSS , , element.classname, classname, , element.

+3

All Articles