Bug Tracker

Ticket #11785 (closed bug: patchwelcome)

Opened 2 years ago

Last modified 20 months ago

.findOne() method

Reported by: dmethvin Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.2
Keywords: Cc:
Blocking: Blocked by:

Description

There would certainly be a performance benefit to this on platforms that support querySelector() which is most of them these days.

 http://forum.jquery.com/topic/why-there-is-no-jquery-findone-method-or-smth-similar

Also, perhaps we could automatically change $("anything:first") to use querySelector()?

Change History

comment:1 Changed 2 years ago by rwaldron

+1 to this

comment:2 Changed 2 years ago by rkatic

I think findFirst() is more appropriate then findOne() for several reasons: 1) It is more similar to the .first() method and the :first filter. 2) it removes any doubt in case of not simple selectors. Consider ".boo, .foo". We expect the ".foo" element if it precedes the ".boo" one, becouse it is how querySelector() works.

So, I suggest three two additions:

  1. jQuery.findFirst(), that behaves like jQuery.find("...")[0] || null
  2. jQuery.fn.findFirst()...
  3. jQuery.first(), that returns jQuery( jQuery.findFirst("...") )

Note how those additions would play nicely with the existent API. For example:

  • $(".foo").find(".boo:first") or $(".foo").findFirst(".boo")
  • $(".boo").first() or $.first(".boo")

EDIT: Rethinking, I come with the conclusion that jQuery.first() would introduce confusion to the API, since .first() is for "filtering" and not for "searching"...

Last edited 2 years ago by rkatic (previous) (diff)

comment:3 Changed 2 years ago by rwaldron

After some consideration and re-reading the forum post, I dont think this makes sense. jQuery's API expects jQuery objects, querySelector is a node, not a list of nodes - so it wouldn't work with the current API anyway... Unless we shove it into a jQuery object, then we're back to good ol' jQuery(selector)

comment:4 Changed 2 years ago by dmethvin

I did a quick jsperf to see if it seemed worth the effort. I thought for sure that it would be. Now I am not so sure, can anyone see something wrong here?

 http://jsperf.com/findone-vs-findall

Once these are wrapped in the overhead of Sizzle I doubt they will be that much different.

comment:5 Changed 2 years ago by dmethvin

  • Status changed from new to closed
  • Resolution set to patchwelcome

Per the jsperf I did, it doesn't seem worth adding this method since it's not faster. If someone wants to come up with a full implementation and perf tests proving otherwise we can reopen.

comment:6 Changed 20 months ago by jquery@…

Putting aside performance for a second, am I the only one that often will look for a single element on the page?

I typically either want to process any number of entries (0, 1 or more), or 1; in the later case, a findOne would be helpful if it raised an exception if none is found.

comment:7 Changed 20 months ago by dmethvin

Not everything needs to be built into jQuery. That's why we have plugins.

jQuery.fn.findOne = function( selector ) {
   var result = this.find( selector );
   if ( this.length != 1 ) {
      jQuery.error( "Didn't find one " + selector );
   }
   return this;
};

 http://jsfiddle.net/e4JdY/

Note: See TracTickets for help on using tickets.