Skip to main content
Participating Frequently
November 6, 2020
Answered

Within CFC cfinvoke function A or call function B

  • November 6, 2020
  • 1 reply
  • 249 views

I'm looking for a best practice and have not found one as yet. Within a CFC I have a function A that needs results from function B. In the past, I have done both call (cfset) and cfinvoke. For memory or speed performance is there a clearly best approach? (oversimplified example here)

 

<cffunction name="functionB" access="private" output="no" returntype="numeric">
   <cfargument name="myArg1" type="numeric" required="yes" >
   <cfargument name="myArg2" type="numeric" required="yes" >

   <cfset var retVal = myArg1 + myArg2>

   <cfreturn retVal>
</cffunction>

<cffunction name="functionA" access="public" output="no" returntype="void">

	<!---better this way--->
   <cfset var sum1 = functionB(1,1)>

	<!---or no difference---> 
   <cfinvoke method="functionB" returnvariable="sum2">
      <cfinvokeargument name="myArg1" value="1"/>
      <cfinvokeargument name="myArg2" value="1"/>
   </cfinvoke>

   
   <!---db query action here--->
</cffunction>

 

    This topic has been closed for replies.
    Correct answer BKBK

    Performance: the 2 function invocations have more or less the same performance. There is no significant difference in the time it takes for ColdFusion to run them. 

     

    Best-practice: one of the best-practices in software development is simplicity. Simplicity is not only craftsmanship, but is also a practical necessity. It has been estimated that 60 to 70 percent of the lifetime cost of an application will go to maintenance. Simplicity means ease of maintenance, hence lower costs in the long run.

     

    On that basis, sum1 is preferable.

     

    You can see for yourself that sum1 and sum2 have similar performance. Do the following test.

     

    1) Place the files test.cfm and Test.cfc in the same directory under the web root.

    2) Replace the line <!--- ***** ---> with the code

     

    <cfset var sum1 = functionB(1,1)>

     

    3) Run test.cfm 3 times and record the results.

    4) Replace the line <cfset var sum1 = functionB(1,1)> with the code

     

    <cfinvoke method="functionB" returnvariable="sum2">
          <cfinvokeargument name="myArg1" value="1"/>
          <cfinvokeargument name="myArg2" value="1"/>
       </cfinvoke>

     

    5) Run test.cfm 3 times and record the results.

    6) Compare these results with those you obtained earlier in step 3). You will see that there is no significant difference.

     

     

    test.cfm
    <cfset testObject = createobject("component", "Test")>
    <cfset startTime = getTickCount()>
    
    <cfoutput>
    <cfloop index="i" from="1" to="300000">
    	<cfset testObject.functionA()>
    	<cfif i mod 100000 eq 0>
    	Execution time after #i# calls: #(getTickCount() - startTime)/1000# seconds <br>
    	</cfif>
    </cfloop>
    </cfoutput>
    
    
    Test.cfc
    <cfcomponent>
    <cffunction name="functionB" access="private" output="no" returntype="numeric">
       <cfargument name="myArg1" type="numeric" required="yes" >
       <cfargument name="myArg2" type="numeric" required="yes" >
    
       <cfset var retVal = myArg1 + myArg2>
    
       <cfreturn retVal>
    </cffunction>
    
    <cffunction name="functionA" access="public" output="no" returntype="void">
    <!--- ***** --->
    </cffunction>
    </cfcomponent>

     

     

     

    1 reply

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    November 11, 2020

    Performance: the 2 function invocations have more or less the same performance. There is no significant difference in the time it takes for ColdFusion to run them. 

     

    Best-practice: one of the best-practices in software development is simplicity. Simplicity is not only craftsmanship, but is also a practical necessity. It has been estimated that 60 to 70 percent of the lifetime cost of an application will go to maintenance. Simplicity means ease of maintenance, hence lower costs in the long run.

     

    On that basis, sum1 is preferable.

     

    You can see for yourself that sum1 and sum2 have similar performance. Do the following test.

     

    1) Place the files test.cfm and Test.cfc in the same directory under the web root.

    2) Replace the line <!--- ***** ---> with the code

     

    <cfset var sum1 = functionB(1,1)>

     

    3) Run test.cfm 3 times and record the results.

    4) Replace the line <cfset var sum1 = functionB(1,1)> with the code

     

    <cfinvoke method="functionB" returnvariable="sum2">
          <cfinvokeargument name="myArg1" value="1"/>
          <cfinvokeargument name="myArg2" value="1"/>
       </cfinvoke>

     

    5) Run test.cfm 3 times and record the results.

    6) Compare these results with those you obtained earlier in step 3). You will see that there is no significant difference.

     

     

    test.cfm
    <cfset testObject = createobject("component", "Test")>
    <cfset startTime = getTickCount()>
    
    <cfoutput>
    <cfloop index="i" from="1" to="300000">
    	<cfset testObject.functionA()>
    	<cfif i mod 100000 eq 0>
    	Execution time after #i# calls: #(getTickCount() - startTime)/1000# seconds <br>
    	</cfif>
    </cfloop>
    </cfoutput>
    
    
    Test.cfc
    <cfcomponent>
    <cffunction name="functionB" access="private" output="no" returntype="numeric">
       <cfargument name="myArg1" type="numeric" required="yes" >
       <cfargument name="myArg2" type="numeric" required="yes" >
    
       <cfset var retVal = myArg1 + myArg2>
    
       <cfreturn retVal>
    </cffunction>
    
    <cffunction name="functionA" access="public" output="no" returntype="void">
    <!--- ***** --->
    </cffunction>
    </cfcomponent>