dom append or dom-fragment append

JavaScript performance comparison

Test case created by Kyle Simpson

Info

Testing/profiling the code patterns from the Script Junkie article "(pre)Maturely Optimize Your JavaScript"

http://msdn.microsoft.com/en-us/scriptjunkie/gg622887.aspx

Snippet comparison #7

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var list_count = 0,
      the_list = [];
 
  for (var i = 0; i < 75; i++) {
   the_list[i] = Math.random();
  }
 
 
  function makeUL_1(list) {
   $("<ul></ul>").attr({
    "id": "my_list" + list_count
   }).appendTo("body");
 
   for (var i = 0; i < list.length; i++) {
    $("<li></li>").text(list[i]).appendTo("#my_list" + list_count);
   }
   $("#my_list" + list_count).hide();
   list_count++;
  }
 
  function makeUL_2(list) {
   var $ul = $("<ul></ul>").attr({
    "id": "my_list" + list_count
   });
 
   for (var i = 0; i < list.length; i++) {
    $("<li></li>").text(list[i]).appendTo($ul);
   }
   $ul.appendTo("body");
   $ul.hide();
   list_count++;
  }
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
dom append
makeUL_1(the_list);
pending…
dom-fragment append
makeUL_2(the_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. Here’s a list of current revisions for this page:

0 comments

Add a comment