jQuery Element Removed?

JavaScript performance comparison

Revision 28 of this test case created by Jaakko Salomaa

Info

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

Updated to jquery 1.9.1.

Preparation code

<div id="element"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    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);
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
closest 'html' inDom
inDom.closest('html');
pending…
closest 'html' removed
removed.closest('html');
pending…
jQuery.contains inDom
jQuery.contains(document.documentElement, inDom[0]);
pending…
jQuery.contains removed
jQuery.contains(document.documentElement, removed[0]);
pending…
jQuery.contains short inDom
jQuery.contains(document, inDom[0]);
pending…
jQuery.contains short removed
jQuery.contains(document, removed[0]);
pending…
isElementInDom(inDom[0])
isElementInDOM(inDom[0]);
pending…
isElementInDom(removed[0])
isElementInDOM(removed[0]);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment