diff --git a/page/performance/append-outside-loop.md b/page/performance/append-outside-loop.md index d1adfa1c..b0356d33 100644 --- a/page/performance/append-outside-loop.md +++ b/page/performance/append-outside-loop.md @@ -36,17 +36,19 @@ $.each( myArray, function( i, item ) { $( "#ballers" )[ 0 ].appendChild( frag ); ``` -Another simple technique is to build up a string during each iteration of the loop. After the loop, just set the HTML of the DOM element to that string. +Another simple technique is to build up a string during each iteration of the loop. After the loop, just set the HTML of the DOM element to that string. Because Javascript strings are immutable, it is most performant to build the buffer in an array first. ``` -var myHtml = ""; +var myHtml = []; $.each( myArray, function( i, item ) { - myHtml += "
  • " + item + "
  • "; + myHtml[i] = "
  • " + item + "
  • "; }); +myHtml.join(''); + $( "#ballers" ).html( myHtml ); ```