Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trycf/cfab5b39b1f2828700583247b4c1def4 to your computer and use it in GitHub Desktop.
Save trycf/cfab5b39b1f2828700583247b4c1def4 to your computer and use it in GitHub Desktop.
TryCF Gist
<cfscript>
num_iterations = 1*1000*1000;
attr = {
key1 = "key1",
key2 = ""
};
start_time = getTickCount();
for(i=1; i<=num_iterations; i++) {
if (structKeyExists(attr, "key1") and len(trim(attr.key1)) gt 0) {
//do stuff here
}
if (structKeyExists(attr, "key2") and len(trim(attr.key2)) gt 0) {
//do stuff here
}
if (structKeyExists(attr, "key3") and len(trim(attr.key3)) gt 0) {
//do stuff here
}
}
end_time = getTickCount();
tot_time = end_time - start_time;
writeDump("num_iterations=#num_iterations# in #tot_time#ms - with native function");
/********************************************************/
start_time = getTickCount();
for(i=1; i<=num_iterations; i++) {
if (structParamExists(attr, "key1")) {
//do stuff here
}
if (structParamExists(attr, "key2")) {
//do stuff here
}
if (structParamExists(attr, "key3")) {
//do stuff here
}
}
end_time = getTickCount();
tot_time = end_time - start_time;
writeDump("num_iterations=#num_iterations# in #tot_time#ms - with custom function");
</cfscript>
<cffunction name="structParamExists" returntype="boolean" output="false">
<cfargument name="args" type="struct" required="true">
<cfargument name="key" type="string" required="true">
<cfscript>
if(structKeyExists(arguments.args, arguments.key) and len(trim(arguments.args["#arguments.key#"])) gt 0){
return true;
}else{
return false;
}
</cfscript>
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment