As I have mentioned before, or as anyone who practices OO in Coldfusion will know, that object instantiation in Coldfusion has quite a bit of overhead.
I’ve seen a few thoughts about how to best work around this, such as the above post on Java vs CFC instantiation by Mark Drew and the ideas presented by Peter Bell with regards to iterating business objects which I’ve mentioned before.
But to be honest I’ve not explored either of these in any great detail, although I really wish I could devote some time to looking at them.
Another, and easier and more instant, solution appears to have been suggested by Brandon over at devnulled in his post on how the Coldfusion compiler works in the following aside:
A related thing to note that he doesn't cover is how much faster it is to instantiate objects in cfscript rather than CFML on a percentage basis. Just some food for thought. I can do a follow-up post on this in particular topic if anyone is interested.
Of course we’re interested in a follow up post. After reading this I spent a few minutes testing this out with a few different CFCs from our model; beans, gateways etc.
I was only looking at the average time of instantiation in either 100 and 1000 iterations and although I did see a tiny improvement in the average time in cfscript I won’t be going out and rewriting all the code that uses cfset to instantiate CFCs just yet.
I’m hoping that Brandon has some hidden trick or information up his sleeve and I’m dying to read a post on it (and I’d say as much in the comments on this post if it would let me, every time I try it doesn’t get added to the post) and find out there really is a marked difference between the two.
Personally I find it easier to read ECMAScript syntax rather than the tags in Coldfusion, so I use it wherever I can already. But if there is a speed benefit for using cfscript when instantiating CFCs then I can use that as leverage to get the rest of the team at work to start to use cfscript wherever possible. Which would be nice.
Update: I’ve just found a post on the Coldfusion compiler over at Absolutely No Machete Juggling that goes into detail about exploring the differences between the resultant compiled Coldfusion code in a simple test (not related to CFC instantiation). The important notes that may apply to CFC instantiation appear towards the bottom of the post:
So there you have it, the SetVariable implementation is slow…
CFScript [when compiled] doesn’t use SetVariable, so if you’re doing heavy loop code like this, I’d recommend writing it using CFScript rather than CFML.