jquery html vs empty vs innerHTML (v17)

Revision 17 of this benchmark created on


Preparation HTML

<ul id="my_list">
</ul>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
  $(function() {
    var i = 0,
        $ul = $('#my_list'),
        $li = $('<li />').html('list item');

    while (i < 100) {
      $ul.append($li.clone());
      i++;
    }
  });


  function empty(element) {
    var i;
    for (i = element.childNodes.length - 1; i >= 0; i--)
    element.removeChild(element.childNodes(i));
  }

  function empty2(element) {
    while (element.childNodes.length > 0)
    element.removeChild(element.childNodes(0));
  }

  function empty3(element) {
    var child;
    while (child = element.lastChild) {
      element.removeChild(child);
    }
  }
  function empty4(element){
    while(element.firstChild){element.removeChild(element.firstChild);}
  }
  function empty5(element){
    //Expected this to be much faster, however it turns out
    //try-catch is a huge performance hit.
    try{
      while(1){
        element.removeChild(element.lastChild);
      }
    }catch(e){        
      //eat this
    }
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
innerHTML
document.getElementById('my_list').innerHTML = '';
ready
jquery html
$('#my_list').html('');
ready
jquery empty
$('#my_list').empty();
ready
custom empty function
empty(document.getElementById('my_list'))
ready
custom empty function 2
empty2(document.getElementById('my_list'))
ready
custom empty function 3
empty3(document.getElementById('my_list'))
ready
custom empty function 4
empty4(document.getElementById('my_list'))
ready
custom empty function 5
empty5(document.getElementById('my_list'))
ready

Revisions

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