Remove v.s. Detach vs hide vs empty vs ""

JavaScript performance comparison

Revision 42 of this test case created by

Preparation code

<script src="https://code.jquery.com/jquery-git2.min.js"></script>

<div id="html">
<div class="sup"></div><div class="sup"></div>
<div class="sup"></div><div class="sup"></div>
<div class="sup"></div><div class="sup"></div>
</div>




      
<script>
Benchmark.prototype.setup = function() {
  var $cl = $('#html').html();
  var $html = $('#html');
  

};

Benchmark.prototype.teardown = function() {
  // need something like this to reset target
  $('#html').empty().html($cl);

};
</script>

Preparation code output

<div id="html"> <div class="sup"></div><div class="sup"></div> <div class="sup"></div><div class="sup"></div> <div class="sup"></div><div class="sup"></div> </div>

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
$('.sup').remove();
pending…
Hide
$('.sup').hide();
pending…
detach
$('.sup').detach();
pending…
html('')
$('#html').html('');
pending…
empty()
$('#html').empty();
pending…
innerHTML = null
$("#html")[0].innerHTML = null;
pending…
hybrid
var el = $("#html")[0];
while (el.firstChild) { el.removeChild(el.lastChild) }
pending…
.find
$html.find('.sup').remove();
pending…
children().remove()
$html.children().remove();
pending…
1
$(".sup").toArray()
pending…
2
$("#html").children().toArray()
pending…
3
$.makeArray($(".sup"))
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