cfloop vs cfoutput on queries
Asked Answered
J

4

9

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?

Junkojunkyard answered 8/10, 2012 at 13:36 Comment(0)
J
12

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.

Jenn answered 8/10, 2012 at 13:45 Comment(4)
In ColdFusion 10 you can now do grouped output.Elk
Nice, I take it you mean grouped cfloops bennadel.com/blog/…Jenn
at cfObjective this year Micah (Railo dev) said everything is compiled to byte code, so having 20 <cfoutput> on your page vs 1 doesn't affect performance (I hope I used the correct terminology)Pedagogics
I'm not sure the conclusion is sound. It really depends how the compiler compiles the CFML pages to bytecode, but I do agree that in all likelihood the compiler will at the very least remove unnecessary instructions to the JVM. This will make up for most cases of inefficient CFML.Jenn
I
3

One good reason to use cfloop instead of cfoutput is if you need to loop a query output within another query output cfoutput does not support nested query outputting. You can however get away with it using cfloops. So:

<cfoutput query="test1">
   #test1ID#
   <cfoutput query="test2">
      #test2ID#
   </cfoutput>
</cfoutput>

does not work, but if you replace the cfoutputs with cfloops, it will.

As of CF10, with the ability to group cfloops, that's the only remaining functional difference. They both perform the same.

Impetuosity answered 8/10, 2012 at 15:34 Comment(0)
D
2

I believe it's all the same as performance, Ben Forta

And the rest is pretty much personal preference as far as how you "like" to work with your loop. Keep in mind you should always scope your variables, but inside a cfoutput loop that would be especially important since the query fields "could" be referenced without referring to their scope.

one reason you may prefer the cfloop approach would be if you needed to "escape" cfoutput during your loop for any reason. I have run into that several times, so I generally prefer cfloop.

Denticle answered 8/10, 2012 at 13:46 Comment(0)
P
1

There wouldn't be a performance difference using either method, it depends on your coding style really. If you put a <cfoutput> at the top and bottom of every page then using <cfloop> will work great. If you use multiple <cfoutput> and only place where they are needed that works as well.

I personally put <cfoutput> only where they are necessary, but I wouldn't say that's more correct than placing them at the top and bottom of the page.

Pedagogics answered 8/10, 2012 at 13:44 Comment(1)
You forgot to mention the ability to be able to use group="" only with cfoutput (Railo 4 allows group on loop).Cheremkhovo

© 2022 - 2024 — McMap. All rights reserved.