If I have a CSS class which I only ever apply to form elements, eg:
Which of these two jQuery select
Edit: The results below are for jQuery 1.2.6, probably in Firefox 3.5. Speed improvements in browsers and jQuery itself have pretty much rendered this information obsolete.
I just wrote a quick benchmarking test:
$('form.myForm')
10000 times took 1.367s$('.myForm')
10000 times took 4.202s (307%)$('form.myForm')
10000 times took 1.352s$('.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 tags and 4
s, runs the two different tests, removes the
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.