jQuery Element Removed? (v28)

Revision 28 of this benchmark created by Jaakko Salomaa on


Description

Checks if a DOM element has been removed; returns true if in DOM, false if removed.

Updated to jquery 1.9.1.

Preparation HTML

<div id="element"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Setup

var removed = $('#element').remove();
    var inDom = $('#tests');
    
    var isElementInDOM = (function($) {
      var docElt = document.documentElement, find,
          contains = docElt.contains ?
            function(elt) { return docElt.contains(elt); } :
    
            docElt.compareDocumentPosition ?
              function(elt) {
                return docElt.compareDocumentPosition(elt) & 16;
              } :
              ((find = function(elt) {
                  return elt && (elt === docElt || find(elt.parentNode));
              }), function(elt) { return find(elt); });
    
      return function(elt) {
        return !!(elt && ((elt = elt.parent) == docElt || contains(elt)));
      };
    })(jQuery);

Test runner

Ready to run.

Testing in
TestOps/sec
closest 'html' inDom
inDom.closest('html');
ready
closest 'html' removed
removed.closest('html');
ready
jQuery.contains inDom
jQuery.contains(document.documentElement, inDom[0]);
ready
jQuery.contains removed
jQuery.contains(document.documentElement, removed[0]);
ready
jQuery.contains short inDom
jQuery.contains(document, inDom[0]);
ready
jQuery.contains short removed
jQuery.contains(document, removed[0]);
ready
isElementInDom(inDom[0])
isElementInDOM(inDom[0]);
ready
isElementInDom(removed[0])
isElementInDOM(removed[0]);
ready

Revisions

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