views:

12299

answers:

7

If I have a CSS class which I only ever apply to form elements, eg:

<form class="myForm">

Which of these two jQuery selectors is most efficient, and why?

a) $('form.myForm')

b) $('.myForm')
+17  A: 

I just wrote a quick benchmarking test:

  • On a page with 4 forms and about 100 other elements:
    • Using $('form.myForm') 10000 times took 1.367s
    • Using $('.myForm') 10000 times took 4.202s (307%)
  • On a page with only 4 forms and no other elements:
    • Using $('form.myForm') 10000 times took 1.352s
    • Using $('.myForm') 10000 times took 1.443s (106%)

It appears that searching for elements of a particular name is much quicker than searching all elements for a particular class.

Edit: Here's my test program: http://jsbin.com/ogece

The program starts with 100 <p> tags and 4 <form>s, runs the two different tests, removes the <p> tags and runs the test again. Strangely, when using this technique, form.myForm is slower. Take a look at the code for yourself and make of it what you will.

nickf
maybe provide the test cases on pastebin.me or jsbin.com nickf?
redsquare
Possibly also dependent on the browser and which CSS selectors are natively supported.
El Yobo
+6  A: 

The first selector should be faster because jQuery can use the built-in function "getElementsByTagName" to reduce the number of elements it needs to filter. The second one has to get all the elements in the DOM and check their class.

Matthew Crumley
+1  A: 

form.myForm IMO is much quicker as it only needs to be look at a subset/filtered set of elements and would not need to iterate the whole document.

GONeale
+2  A: 

Try slickspeed where you can see the rough speeds of selectors across a multiple of js libs including jquery.

redsquare
cool site! jQuery wins! though, it doesn't test .className vs tagName.className :(
nickf
+1  A: 

The first example goes a LOT faster when used with a context. The second example goes faster as well, but not by much. I expanded your example to compare with a context:

http://jsbin.com/uluwe

enobrev
A: 

enobrev, Interesting. I just ran your example but using jquery 1.3 beta 2 here

results.... (1.2.6 speed in brackets)

// With 100 non-form elements and Context:
$('.myForm', '#someContainer') : 4063ms (3707ms)
$('form.myForm', '#someContainer') : 6045ms (4644ms)

// With 100 non-form elements: 
$('.myForm') : 3954ms (25086ms)
$('form.myForm') : 8802ms (4057ms)

// Without any other elements with Context: 
$('.myForm', '#someContainer') : 4137ms (3594ms)
$('form.myForm', '#someContainer') : 6627ms (4341ms)

// Without any other elements: 
$('.myForm') : 4278ms (7303ms) 
$('form.myForm') : 8353ms (4033ms)
redsquare
Woah, forget the context for 1.3!! Also, that's a pretty big difference for the 2nd test (between versions). Looks like jquery lost a bit of speed for form.myForm, but the huge gain for .myForm will likely make a bigger difference overall.
enobrev
A: 
Ionut Staicu
searching for elements by tag name is faster than searching for elements by class name. form.myForm means you only need to search through a smaller subset of the elements. If you only have a couple of elements, then you're doing two searches and it might be slower, but in real life form.myForm wins.
nickf
take a look at profiler results (or do your own tests) then tell me this. Also, even John Resig confirmed this ;)
Ionut Staicu
also, the test is made on real life, not on a test site. you can try even here, on SO to be sure i'm telling the truth :)
Ionut Staicu
but your using an id selector so of course if you rpefix with tagName it is slower as the lib cannot use getElementById!!!
redsquare
check and change your test to use the .class selector. you will see the difference
redsquare
I can't believe that the most complete reply got negative rating and the most WRONG got TWO positive rating. Anyhow, would be nice if i got some arguments for that down vote...
Ionut Staicu
you have them above....your using id selectors in your test....not .class
redsquare
just read the posts below and go to the link discussed to see .class IS slower than element.class http://jsbin.com/uluwe
redsquare
for iron proof check http://pastebin.me/4963d9f952364
redsquare