Test name | Executions per second |
---|---|
isInDOM1 | 3228366.8 Ops/sec |
isInDOM2 | 6147004.5 Ops/sec |
<div id="test-div"></div>
function isInDOM1(oHTMLElement) {
if (oHTMLElement === document.body || oHTMLElement === document ) {
return true;
} else {
try {
return document.body.contains(oHTMLElement);
} catch(ex) {
// Sometimes in Firefox element is anonymous div around input.
// Throws error "TypeError: Argument 1 of Node.contains does not implement interface Node"
// See https://bugzilla.mozilla.org/show_bug.cgi?id=208427
return false;
}
}
}
function isInDOM2(oHTMLElement) {
var root = oHTMLElement.ownerDocument;
if (!root) { return false; }
if ( oHTMLElement.ownerDocument.documentElement === document.documentElement ){ return true; }
return false;
}
var div = document.getElementById('test-div');
isInDOM1(div);
isInDOM2(div);