I have a loop within a loop leading to longer loading time of my page. With more data the loading time can go up to 10,000 ms, which is about 10 seconds. How can I write it for faster load time? The code I have so far:
<cfloop from="#endDate#" to="#startDate#" index="i" step="#CreateTimeSpan(+1,0,0,0)#">
<cfset loopdate = dateformat(i,'mmm dd')>
<!---Loop the number of likes for each day--->
<cfset daylike = 0>
<cfset dayretweet = 0>
<cfset tweetrec = now()>
<cfloop from = 1 to = #arraylen(DeserializeJSON(cfhttp.fileContent))# index = "i">
<cfset tweetrec = dateformat(DeserializeJSON(cfhttp.fileContent)[i].created_at,'mmm dd')>
<cfif tweetrec IS loopdate>
<cfset daylike = daylike + DeserializeJSON(cfhttp.fileContent)[i].favorite_count>
<cfset dayretweet = dayretweet + DeserializeJSON(cfhttp.fileContent)[i].retweet_count>
</cfif>
</cfloop>
<!---add the favourites to array--->
<cfset myarray = ArrayAppend(likes, "#daylike#")>
<!--- Append dates to dates array --->
<cfset myarray = ArrayAppend(dates, "#loopdate#")>
<!---Append retweets to retweets array --->
<cfset myarray = ArrayAppend(retweetarr, "#dayretweet#")>
</cfloop>
[period of change](eg: daily) to do the processing and put the resultant data in the application scope, or some cache or the like.DeserializeJSON(cfhttp.fileContent)four times in your code. It would be much more efficient to do that once before the first loop starts and store it in a new variable. You're currently doing the same process multiple times on the same string which isn't required. Also noticed your inner and outer loops both haveindex="i"they should be different.Deserializeoutside the loop, and then turn this into 1 loop instead of 2. You also assign the result ofArrayAppend(which is just a true/false) to a variable which is completely ignored. You may be able to save a few cycles by switching to<cfset ArrayAppend(likes, "#daylike#")>, but this will be minor in comparison.