isInDocument

JavaScript performance comparison

Test case created by terodox

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const elementWithShadowInDom = document.createElement('div');
  elementWithShadowInDom.attachShadow({mode :'open'});
  const childElementInsideShadowInDom = document.createElement('div');
  elementWithShadowInDom.shadowRoot.appendChild(childElementInsideShadowInDom);
  document.body.appendChild(elementWithShadowInDom);
  
  const elementWithShadowNOTDom = document.createElement('div');
  elementWithShadowNOTDom.attachShadow({mode :'open'});
  const childElementInsideShadowNOTDom = document.createElement('div');
  elementWithShadowNOTDom.shadowRoot.appendChild(childElementInsideShadowNOTDom);
  
  const elementInDom = document.createElement('div');
  elementInDom.attachShadow({mode :'open'});
  const childElementInDom = document.createElement('div');
  elementInDom.shadowRoot.appendChild(childElementInDom);
  document.body.appendChild(elementInDom);
  
  const elementNOTDom = document.createElement('div');
  elementNOTDom.attachShadow({mode :'open'});
  const childElementNOTDom = document.createElement('div');
  elementNOTDom.shadowRoot.appendChild(childElementNOTDom);
  
  function isInDocument(element) {
      if(document.body.contains(element)) {
          return true;
      }
      let currentElement = element;
      while(currentElement && currentElement.parentNode) {
          if(currentElement.parentNode === document) {
              return true;
          } else if(DocumentFragment && currentElement.parentNode instanceof DocumentFragment) {
              currentElement = currentElement.parentNode.host;
          } else {
              currentElement = currentElement.parentNode;
          }
      }
      return false;
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Shadow DOM element is in DOM
isInDocument(childElementInsideShadowInDom);
pending…
Shadow DOM element not in DOM
isInDocument(childElementInsideShadowNOTDom);
pending…
Regular DOM element in DOM
isInDocument(childElementInDom);
pending…
Regular DOM element NOT in DOM
isInDocument(childElementNOTDom);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments