Jquery syntax question

Hi there,
What is the difference between the following two versions of checking if a radio button is checked.  The second version seems concise but is there any difference. Thanks.



Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
there is no difference in the result but the latter one has a better Performance as it can directly use the getElementById functionality.

When you search the DOM via attributes (not classes) then is takes always more time/resources ...

Julian HansenCommented:
Just as a matter of interest here are some performance times on the above

Running 10,000 iterations of each of the following selector mechanisms

A: $("input[id='SelectorID']").is(":checked")
B: $("#SelectorID").is(":checked")
C: $("#SelectorID:checked").length > 0

And finding the average of 10 iterations of each (in Microseconds) when there is only 1 radio in the DOM gives approximately the following results (will vary from computer to computer)
A: 54 microseconds
B: 15 microseconds
C: 34 microseconds

When adding an additional 2000 radios to the page the times change to
A: 253 microseconds
B: 15 microseconds
C: 35 microseconds

So B And C unchanged A changes dramattically.

But we are talking about iterating 100,000 times over a DOM with 2001 radio's in and hitting 253 microseconds on a client computer - not exactly a major concern - but for the purists out there (me included) - in line with Rainer's post the direct (id) select is always going to be quicker and IMHO a cleaner solution than the other two.

If you are interested in how the performance was measured the code is here

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Addendum - above figures are for FireFox - in Chrome the times for B and C are switched C is faster than B and both are faster than their equivalents on FF BUT A is significantly slower on A (461 for the 1000 test)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.