jquery html vs empty vs innerHTML

JavaScript performance comparison

Revision 9 of this test case created by Marcin

Preparation code

<ul id="my_list">
</ul>
<script src="https://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);
    }
  }
</script>
    

Preparation code output

<ul id="my_list"> </ul>

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
innerHTML
document.getElementById('my_list').innerHTML = '';
pending…
jquery html
$('#my_list').html('');
pending…
jquery empty
$('#my_list').empty();
pending…
custom empty function
empty(document.getElementById('my_list'))
pending…
custom empty function 2
empty2(document.getElementById('my_list'))
pending…
custom empty function 3
empty3(document.getElementById('my_list'))
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