cfloop vs cfoutput on queries

Abbadon picture Abbadon · Oct 8, 2012 · Viewed 8.5k times · Source

I develop using ColdFusion and wanted to know what is the best strategy to loop over large query result set. Is there any performance difference between using cfloop and cfoutput? If not, is there any reason to prefer one over the other?

Answer

Stuart Wakefield picture Stuart Wakefield · Oct 8, 2012

I believe that there used to be. I think this difference has been tackled, the best bet is to do a test for each to test in you specific use case.

<cfset t = GetTickCount()/>
<cf... query="qry">
  <!--- Do something --->
</cf...>
<cfset dt = GetTickCount() - t/>
<cfdump var="#dt#"/>
<!--- 
If the differences are small you can use java.lang.System.nanoTime() instead 
--->

There are some notable differences though. cfoutput can do grouped loops, which cfloop cannot.

<cfoutput query="qry" group="col">
  <!--- Loops once for each group --->
  <cfoutput>
    <!--- Loops once for each record within the group --->
  </cfoutput>
</cfoutput>

For cfoutput you can specify the startrow and the maxrows (or the count) to paginate your result. For cfloop you have to specify the endrow index instead of the count.

Also you cannot use cfoutput for a query nested within an existing cfoutput tag, you will need to end the containing cfoutput first.