Question: Whats faster when using jquery selectors .classname or div.classname

Question

Whats faster when using jquery selectors .classname or div.classname

Answers 1
Added at 2017-01-03 13:01
Tags
Question

Is it faster to use

$(".classname").

or add the tag to look for as well

$("div.classname")

I think its the classname as i'm sure jquery will just loop through to get all classnames where the 2nd one will first get all the div tags and then loop through that subset to get the classnames. Thats what i think anyway.

Anyone know?

Answers
nr: #1 dodano: 2017-01-03 13:01

You can always run your code on jsperf.com to check code sequence performance in different browsers.

About your questions. Chrome run shows that strict selector (tag + class name) is much slower than class selector only

enter image description here

Anyway you can rerun this code in different browsers to use different program flow based on browser type.

https://jsperf.com/jquery-class-vs-tag-qualfied-class-selector/2

Source Show
◀ Wstecz