To $('#foo p') or to $('p', $('#foo'))

24 February 2009   2 comments   Javascript

http://groups.google.com/group/jquery-en/msg/ec43a643e0f0bd54

Mind That Age!

This blog post is 9 years old! Most likely, its content is outdated. Especially if it's technical.

For the performance interested jQuery users please check out this thread

For the impatient, read Stephens reply He benchmarked what I asked and concluded that $("p", $("#foo")) is much faster in jQuery 1.3.2. I've been coding this style in jQuery for all recent projects so I'm happy with this outcome.

UPDATE

John Resig himself joined in on the discussion and had this to say:

"You should always use $("#foo").find("p") in favor of $("p", $("#foo")) - the second one ends up executing $(...) 3 times total - only to arrive at the same result as doing $("#foo").find("p")."

UPDATE 2

Not only did John join in on the discussion but it also made him work on jQuery 1.3.3 (not yet released at the time of writing) so that it doesn't matter which format you use you get the same performance. See the benchmark here

Comments

h3
Why not simply: $("#foo").find("p");

The syntax is more clear and readable, and jQuery isn't called twice.

Performance wise, they are practically equivalent.
Peter Bengtsson
See my update in the blog post.

Your email will never ever be published


Related posts

Previous:
Propeller Island City Lodge Orange Room 23 February 2009
Next:
Sandisk SSD v/s HDD 04 March 2009
Related by Keyword:
Fastest *local* cache backend possible for Django 04 August 2017
How to do performance micro benchmarks in Python 24 June 2017
Some tips on learning React 04 August 2015
Benchmarking Autocompeter 12 April 2015
django-fancy-cache with or without stats 11 March 2013
Related by Text:
Be very careful with your add_header in Nginx! You might make your site insecure 11 February 2018
jQuery and Highslide JS 08 January 2008
I'm back! Peterbe.com has been renewed 05 June 2005
Anti-McCain propaganda videos 12 August 2008
I'm Prolog 01 May 2007