Bug Tracker

Opened 11 years ago

Closed 11 years ago

#11410 closed enhancement (wontfix)

Optimisation - delegate to $.has for calls to $.closest whicgh are passed a node/jQuery rather than a selector

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

Description

While trying to optimise a bit of code I came across the surprising fact that

jQueryInstance.has(DOMnode)

is 2-3 times as fast as

$(DOMnode).closest(jQueryInstance/DOMnode)

http://jsperf.com/efficient-way-to-find-elements-not-contained-in-another

Could this be used within $.fn.closest to optimise it when a node/jQuery is passed in rather than a selector -

if(typeof selector !== "string) {
   return $.fn.has.call(selector, this);
}

Change History (5)

comment:1 Changed 11 years ago by dmethvin

Owner: set to wheresrhys
Status: newpending

Is that a common case? Do you have an app that is slow because of it? If it's not a bottleneck in real code, it's hard to justify a special case. It makes the code bigger, more complex, and harder to test.

comment:2 Changed 11 years ago by wheresrhys

Status: pendingnew

Couldn't really comment on whether it's a common use case to get the closest node which is a specific node. My use case was in order to exclude matches from within a given node (e.g get all inputs in a page which aren't in a specific form), but then again, for this case direct use of $.has within the $.filter function is available

comment:3 Changed 11 years ago by ajpiano

You can also use jQuery.contains when you are trying to do that check and you have access to actual nodes for the container and containing elements.

comment:4 Changed 11 years ago by addyosmani

Cc: dmethvin added
Priority: undecidedlow

This is looking like a special case to me and as Adam pointed out jQuery.contains could be used for this particular case. @dmethvin do you think there's enough justification to keep this open?

comment:5 Changed 11 years ago by dmethvin

Resolution: wontfix
Status: newclosed

Doesn't seem common enough to add IMO. If someone has some real-life code that is made much happier by this let us know.

Note: See TracTickets for help on using tickets.