I tried in different browsers, and the DOM function below is 3-10 times faster than the selection methods (jQuery or document.querySelectorAll)
function is(parent){
return {
aParentOf:function(child){
var cp = child.parentNode;
if(cp){
return cp.id === parent.id ?
true : is(parent).aParentOf(cp);
}
}
}
}
The call below will return true if A is the parent of D
is(document.getElementById('A')).aParentOf(document.getElementById('D'))
For a few calls, I would use. $('#A #D').length
For very frequent calls, I would use the DOM.
source
share