Frank Fusion

Sunday, 23 September 2007

Loopy Loo

What is the best way to loop a structure in a cfscript block? On discovering the elements() and keys() methods that are inherited from java.util.dictionary, I presumed these would be faster i.e.
<cfscript>
keys = myStruct.keys();
while( keys.hasMoreElements() ){
key = keys.nextElement();
value = myStruct[key];
// do stuff...

}
// or
values = myStruct.elements();
while( values.hasMoreElements() ){
value = values.nextElement();
// do stuff...
}
// or
keys = myStruct.keys();
values = myStruct.elements();
while( keys.hasMoreElements() ){
key = keys.nextElement();
value = values.nextElement();
// do stuff...
}
</cfscript>
Normally, I would use StructKeyArray() and one might conceivably use StructKeyList():
<cfscript>
// StructKeyArray()
keys = StructKeyArray(myStruct);
size = ArrayLen(keys);
for (i=1; i LTE size; i=i+1){
key = keys[i];
value = myStruct[key];
// do stuff...
}

// StructKeyList()
keys = StructKeyList(myStruct);
size = ListLen(keys);
for (i=1; i LTE size; i=i+1){
key = ListGetAt(keys, i);
value = myStruct[key];
// do stuff...
}
</cfscript>
So which is faster? Well, it all depends:

For small structures, say 10 keys, StructKeyArray() is the winner followed closely by the inherited Java methods and then StructKeyList(). The differences in performance aren't huge here, but StructKeyList() is still clearly slower.

As the structure gets larger however, the results change. Using StructKeyList() becomes exponentially slower and the inherited Java methods overtake StructKeyArray() somewhere in the region of a structure with 150 keys.

Conclusion: use StructKeyArray() unless you happen to be dealing with a huge structure and you need to squeeze every bit of performance necessary. I can't see why you should ever use StructKeyList() to loop a structure, perhaps someone could give an example where it is necessary?

On a similar note, I had also presumed that using myArray.size() would be faster than ArrayLen(myArray), it certainly looks prettier to me anyway! It turns out I was wrong. Some magic in ArrayLen() means it well outperforms size(). Still, size() is OO and looks nice ;)

Labels: ,

Saturday, 22 September 2007

Singular URL and FORM variable

After reading this discussion on IsDefined() vs StructKeyExists(), I got to thinking about how one might use the underlying Java methods of ColdFusion structures to combine the URL and FORM variables efficiently (it is related, really).

A quick inspection of the java docs for java.util.Hashtable turned up the putAll() method. With it, you can 'put' one struct into another, overwriting any matching keys.

So, to very quickly and elegantly combine the form and url scopes into a request variable called 'args', we can do:
<cfset request.args = StructNew()>
<cfset request.args.putAll(url)>
<cfset request.args.putAll(form)>
Et voila!

Labels: ,

ColdFusion objects are Java objects...

A good while back I read the following post about using the methods that ColdFusion objects inherit from their Java parents. It prompted me to write a little function for finding out what Java objects any ColdFusion object inherits from:

http://coldfused.blogspot.com/2007/01/extend-cf-native-objects-harnessing.html

<cfscript>
function GetClassHeirarchy(obj)
{
var thisClass = obj.GetClass();
var sReturn = thisClass.GetName();

do{
thisClass = thisClass.GetSuperClass();
sReturn = sReturn & " EXTENDS: #thisClass.GetName()#";
}while(CompareNoCase(thisClass.GetName(), 'java.lang.Object'));

return sReturn;
}
</cfscript>

<!--- so, for example: --->
<cfoutput>#GetClassHeirarchy(StructNew())#</cfoutput>

I think it fairly useless having the function just sitting around and have decided to post results, with links to java and other docs, here on the interweb! You will be able to use most, if not all, the inherited methods that you see in the JavaDocs.

ColdFusion Query
java.lang.Object -> coldfusion.sql.imq.imqTable -> coldfusion.sql.Table -> coldfusion.sql.QueryTable

ColdFusion Struct
java.lang.Object -> java.util.Dictionary -> java.util.Hashtable -> coldfusion.util.FastHashtable -> coldfusion.runtime.Struct

ColdFusion Array
java.lang.Object -> java.util.AbstractCollection -> java.util.AbstractList -> java.util.Vector -> coldfusion.runtime.Array

ColdFusion XML Document Object
java.lang.Object -> org.apache.xerces.dom.NodeImpl -> org.apache.xerces.dom.ChildNode -> org.apache.xerces.dom.ParentNode -> org.apache.xerces.dom.CoreDocumentImpl -> org.apache.xerces.dom.DocumentImpl

That's all for now, will be posting examples of use in the coming hours...

Dom

Labels: ,