jQuery Element Removed? (v15)

Revision 15 of this benchmark created by Eamon Nerbonne on


Description

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

Updated to jquery 1.9.1.

2013-06-12: - added fast isInDom function - now verifies whether the alternatives actually work too in cleanup. (+prevent dead code elimination).

Preparation HTML

<div id="element"></div><div id="tests"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
var isInDom = (function(){ //by Eamon Nerbonne
  var root = document.documentElement;
  return function f(el) { 
    return el === root || !!el && f(el.parentElement); 
  }
})();

var removed = $('#element').remove();
var inDom = $('#tests');
</script>

Setup

OK = false

Teardown


    if(!OK) throw OK;
  

Test runner

Ready to run.

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

Revisions

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