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
Owner: | set to wheresrhys |
---|---|
Status: | new → pending |
comment:2 Changed 11 years ago by
Status: | pending → new |
---|
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
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
Cc: | dmethvin added |
---|---|
Priority: | undecided → low |
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
Resolution: | → wontfix |
---|---|
Status: | new → closed |
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.
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.