isInDocument

JavaScript performance comparison

Revision 4 of this test case created by Andrew Desmarais

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  window.elementWithShadowInDom = document.createElement('div');
  window.elementWithShadowInDom.attachShadow({mode :'open'});
  window.childElementInsideShadowInDom = document.createElement('div');
  window.elementWithShadowInDom.shadowRoot.appendChild(window.childElementInsideShadowInDom);
  document.body.appendChild(window.elementWithShadowInDom);
  
  window.elementWithShadowNOTDom = document.createElement('div');
  window.elementWithShadowNOTDom.attachShadow({mode :'open'});
  window.childElementInsideShadowNOTDom = document.createElement('div');
  window.elementWithShadowNOTDom.shadowRoot.appendChild(window.childElementInsideShadowNOTDom);
  
  window.elementInDom = document.createElement('div');
  window.elementInDom.attachShadow({mode :'open'});
  window.childElementInDom = document.createElement('div');
  window.elementInDom.shadowRoot.appendChild(window.childElementInDom);
  document.body.appendChild(window.elementInDom);
  
  window.elementNOTDom = document.createElement('div');
  window.elementNOTDom.attachShadow({mode :'open'});
  window.childElementNOTDom = document.createElement('div');
  window.elementNOTDom.shadowRoot.appendChild(window.childElementNOTDom);
  
  window.isInDocument = function(element) {
      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
window.isInDocument(window.childElementInsideShadowInDom);
pending…
Regular DOM element in DOM
window.isInDocument(window.childElementInDom);
pending…
Shadow DOM element not in DOM
window.isInDocument(window.childElementInsideShadowNOTDom);
pending…
Regular DOM element NOT in DOM
window.isInDocument(window.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