jQuery .remove vs. .hide vs. detach

JavaScript performance comparison

Revision 16 of this test case created by

Preparation code

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<div id="remove-me">confirm removed</div>
<div id="hide-me">confirm hidden</div>
<div id="detach-me">confirm detatched</div>

<script>
  function init() {
   // The magic init() function will be executed once before every test
   // Still not perfect, but definitely better
   $('<div id="remove-me" />').appendTo('body');
   window.$element = $('#hide-me').show();
   if (window.$detached != null) {
    window.$detached.appendTo('body');
   }
  }
</script>
    

Preparation code output

<div id="remove-me">confirm removed</div> <div id="hide-me">confirm hidden</div> <div id="detach-me">confirm detatched</div> <script> function init() { // The magic init() function will be executed once before every test // Still not perfect, but definitely better $('<div id="remove-me" />').appendTo('body'); window.$element = $('#hide-me').show(); if (window.$detached != null) { window.$detached.appendTo('body'); } } </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
.remove()
$('#remove-me').remove();
pending…
.hide()
$('#hide-me').hide();
pending…
.detach()
$('#detach-me').detach();
pending…
style.display
document.getElementById('hide-me').style.display = 'none';
pending…
.css()
$('#hide-me').css('display', 'none');
pending…
.css() without lookup
window.$element.css('display', 'none');
pending…
style.display with jQuery
$('#hide-me')[0].style.display = 'none'
pending…
style.display without lookup
window.$element[0].style.display = 'none'
pending…
style.display with getElementById && jQuery
$(document.getElementById('hide-me'))[0].style.display = 'none';
pending…
style.display with querySelector
document.querySelector('#hide-me').style.display = 'none';
pending…
.hide(0)
$('#hide-me').hide(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.

0 Comments

confirm removed
confirm hidden
confirm detatched