From d89e85ffa8fc036f5a051a4503be407b87fd68fc Mon Sep 17 00:00:00 2001 From: maschwenk Date: Fri, 23 Oct 2015 17:36:01 -0400 Subject: [PATCH] Update append-outside-loop.md From my research, this method is much faster. --- page/performance/append-outside-loop.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 ); ```