1. cfinclude a file with UDFs on every page that needs the functions
2. store the functions in a CFC located in a shared scope
3. store the UDFs in a shared scope
I want to create a function library that can be used on every page within every application on a server. My primary goals are ease of implementation and low processing overhead.
The setup
The experiment was to take the same function library and time how long it takes to run the code that ensures that the functions are available to the application, using the three methods above. The code segments, listed below, were looped over 25,000 times, and 50 runs were averaged together. All of the code segments were in Application.cfm.
Results
UDFs in server scope: 170 ms
CFC in server scope: 259 ms
cfinclude UDFs: 5912 ms
Note: the first runs, which have the overhead of compilation and instantiation, were excluded. I am less interested in the first person that hits a site after a server restart than I am in the 200,000 people that follow.
Conclusion
If you want a function library available to all the pages in your application, storing the function library in a shared scope is better than using a cfinclude in Application.cfm. UDFs in the server scope consistently showed a slight edge over CFCs in the server scope, but I would not conclude that this means UDFs are faster than CFCs. The compilation and instantiation times are excluded from the results and the check for variable existence in each case should theoretically be identical. Instantiation of a CFC takes longer than a cfinclude, but if you are storing the function library in the server scope, very few people will notice this one-time occurrence. I would assume that there is no significant difference in speed when it comes to accessing and running the functions inside of each type of function library.
The test code that was looped over
CFINCLUDE
<cfinclude template="fcnDebugMX3.cfm">
Note: for this test to work, the function library has lines which delete each function after creation:
<cfset structDelete(variables,'dspCfcatch')>
UDF in server scope
<cfif NOT structKeyExists(server,'debugFcns2')>
<cflock timeout="10" throwontimeout="No" name="debug" type="EXCLUSIVE">
<cfif NOT structKeyExists(server,'debugFcns2')>
<cfinclude template="fcnDebugMX2.cfm">
</cfif>
</cflock>
</cfif>
Note: inside the function library are lines like this:
<cfset server.dspCfcatch=variables.dspCfcatch>
CFC in server scope
<cfif NOT structKeyExists(server,'debugFcns')>
<cflock timeout="10" throwontimeout="No" name="debug" type="EXCLUSIVE">
<cfif NOT structKeyExists(server,'debugFcns')>
<cfset server.debug=createObject("component","includes.debug")>
</cfif>
</cflock>
</cfif>
Mike Chabot
---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' in the message of the email. CFCDev is run by CFCZone (www.cfczone.org) and supported by Mindtool, Corporation (www.mindtool.com). An archive of the CFCDev list is available at www.mail-archive.com/[EMAIL PROTECTED]
