New and cfimport are slower then createobject?

  Follow me: Follow Bruce Kirkpatrick by email subscription Bruce Kirkpatrick on Twitter Bruce Kirkpatrick on Facebook
Tue, Apr 23, 2013 at 3:45PM

ColdFusion 10 and Railo 4 added support for the "new" operator which can be used in conjunction with <cfimport> to allow an alternative form of object creation then using createobject().
 
Today, I noticed the Adobe docs claim that using cfimport and new for cfcs creation is faster then createobject.
 
Well, I use the Railo CFML Engine and I wanted to see if that was true in Railo 4.0.4.004.  It wasn't true in Railo.  The "new" operator is slower then createobject in all my tests.
 
So then I downloaded Coldfusion 10 developer edition and ran the same code and found it's not true on Coldfusion either, at least not when cfcomponent accessors="true".  It also seemed to randomly take longer to load even when accessors were off.  When the "new" operator was faster, it was only 10% faster.  If you are using accessors "new" operator is up to 3 times slower then createobject().
 
One should note that you need accessors enabled for the "new" automatic initialization to work.  That means the best reason to use "new" also causes it to be slower then expected.  Calling an init function manually is still faster then using the "new" operator in Railo 4.
 
Here is what Adobe claims in their documentation:
"You refer to the imported component directly without using a dot-delimited pathname. Execution time for cached components is faster than with CFCs that you do not import."

http://help.adobe.com/en_US/ColdFusion/10.0/Developing/WS61C07B60-3D65-4d71-8F2A-8411D8010E60.html

The performance difference is so small that most apps won't notice the difference using either approach, but if you are doing a large amount of object complex object creation, it doesn't seem like using the "new" operator is a good solution.  You are better off continuing to use createobject or switching to duplicate() on a cached copy of the object.  Many objects can be designed so that duplicate() works and that can be a WAY faster solution.
 
Want to run this test on your installation? Click here to download my test files.  Make sure you enable accessors in the components if you want to test that as well.

Bookmark & Share



Popular tags on this blog

Performance |