My cfscript version of cfparam
Posted by
Brad Wood
Jul 20, 2008 06:16:00 UTC
Some time ago while working in cfscript I had the need to cfparam a bunch of variables. I was building an XML document out of form fields and a number of the fields wouldn't always exist.
At first I made a simple UDF to encapsulate a cfparam tag, but it this code was inside of a loop that beat it over and over again before it was finished and performance just wasn't cutting it. The culprit as it seemed was scope hunting.Before I go too far, I would like to point out there have been many long-winded discussions about performance, what code runs better on what version of CF, and what a proper load test is etc. That is really neither here nor there. I am simply documenting how I solved my problem.
I changed the function to use isdefined() and first, but then still unhappy with the apparent scope hunting and ambiguity I switched to using structkeyexists() and structinsert() and I called the function structparam(). The idea was to be able to set the default value AND get the value out at the same time.
I believe the reason I had so much better performance out of it was because in my specific scenario a majority of my variables were most likely NOT to exist. Since I pass the scope/struct into the function I believe that effectively eliminated the need for CF to search all the scopes.
This was what I came up with.
[code]<cfscript> /** * Function to duplicate the <cfparam> tag within CFSCRIPT. * I attempted to make cfparam faster by eliminating * the evaluate, and using structkeyexists * According to my tests this is MULTUPLE times faster * than the cfparam tag when the variables * being paramed are more likley NOT to exist. * * @param struct The struct. * @param key The name of the key * @param default_value The default value. If not passed, use * @return Returns the value of the variable parammed. * @author Brad Wood * @version 1, November 16, 2006 */ function structparam(struct,key,default_value) { if(not structkeyexists(struct,key)) { structinsert(struct,key,default_value); return default_value; } else { return structfind(struct,key); } } </cfscript> [/code]Usage was as follows:
[code]<cfscript> foo = structparam(attributes,'bar','default value'); </cfscript>[/code]I really wish ColdFusion's structinsert() function did NOT an throw an error when "allow overwrite" is set to no and the key exists. Then it would be the perfect replacement for a cfscript param.
Tags: ColdFusion, Performance
dave
tom muck did this last month http://www.communitymx.com/content/article.cfm?cid=47AE0
Don Q
Tom Muck's article uses IsDefined. Cover's UDF is much more efficient I would imagine.
And While looking at Tom Muck's cfparam UDF, I'm still having a hard time finding where the second argument of "default" is used.
Brad Wood
Yeah, I had trouble following Tom's param function. It does appear to ignore the default value. His function also returns a string, yet his sample usage does not use the returned value.
Don Q
Yeah, noticed that too, but I guess on his example instead of just cfparam(var,param); he could use it as var = cfparam(var,param);
But, I've been thinking. Cant we use something from the GetPageContext? Below is just conceptual, far from working.
<cfset new={random="000"} /> #iif( len(GetPageContext().SymTab_findSimpleName('new.random')), GetPageContext().SymTab_findSimpleName('new.random'), GetPageContext().SymTab_setSimpleName('new.random',RandRange(1,999)) )#
dave
i had troubles with tom's as well. I did see on ben forta site about the new cf committee looking into adding more tags into cfscript & hopefully this will be one of them.