Skip to main content

Bug Tracker

Side navigation

#11410 closed enhancement (wontfix)

Opened February 28, 2012 03:46PM UTC

Closed April 04, 2012 02:15AM UTC

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);
}
Attachments (0)
Change History (5)

Changed February 28, 2012 03:54PM UTC by dmethvin comment:1

owner: → 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.

Changed February 29, 2012 05:48PM UTC by wheresrhys comment:2

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

Changed February 29, 2012 06:16PM UTC by ajpiano comment:3

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.

Changed March 09, 2012 03:31PM UTC by addyosmani comment:4

cc: → dmethvin
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?

Changed April 04, 2012 02:15AM UTC by dmethvin comment:5

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.