Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36
Chrome 117
Mac OS X 10.15.7
Desktop
one year ago
Test name Executions per second
isInDOM1 3228366.8 Ops/sec
isInDOM2 6147004.5 Ops/sec
HTML Preparation code:
AخA
 
1
<div id="test-div"></div>
Script Preparation code:
x
 
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');
Tests:
  • isInDOM1

     
    isInDOM1(div);
  • isInDOM2

     
    isInDOM2(div);