I was writing some code in ColdFusion using CFC components, when I got bit confused on using different ways to create a object of a component.
I would appreciate if someone please let me know which method of creating a object is better.
CreateObject()
, EntityNew()
& New
keyword.
I read through few blogs and got mixed answers, some said Entity New is faster as compared to Create Object.
I also found that the syntactical different is better in EntityNew()
.
I would appreciate if I can get some thoughts on from anyone.
Thanks.
In ColdFusion which way is better to create a component object CreateObject() or EntityNew() or New keyword
827 Views Asked by Jatin Waichal AtThere are 3 best solutions below

If the object is not ORM enabled (i.e. no persistent=true
), use the new
operator. It's clean and readable by many.
Even if I am writing in CFML, I would much prefer <cfset foo = new X()>
over <cfobject>
. I would use <cfinvoke>
only if I need to invoke dynamic method back then when Evaluate()
is the only option, but CF10+ has invoke()
and I do not use <cfinvoke>
anymore, see: https://wikidocs.adobe.com/wiki/display/coldfusionen/Invoke
If the object has persistent=true
, then I may opt for entityNew()
because from what I've learned in the CF9 days: https://stackoverflow.com/a/1349161/35634 it should be more efficient when I need to entitySave() it later.

What's the standard for your development team?
That's pretty much the only question you need answered. "X is faster than Y" can change per platform and server resources. What's faster for someone else may not be faster for you. If you want to use the new syntax, then make that the standard going forward. You don't have to refactor existing instances of createObject()
, at least not right away, but you should standardize on what everyone will use going forward.
Original version, written whilst this question was closed: http://blog.adamcameron.me/2014/12/fucking-stackoverflow-and-new-vs.html
Transcript: This is a very nebulous question, and I am going to vote to close it once I post this.
Unless someone can come up with real-world performance penalties using new,
createObject()
,<cfobject>
or<cfinvoke>
, I'd not bother listening to them. There will not be a meaningful difference. They are dwelling in the realms of micro (and premature ~) optimisation.All those options I list above are slightly different to
entityNew()
, which is specifically intended for creating ORM-based objects. The others are more generic. But, again, performance-wise there will be no real-world consideration here.I do not use tags when I can avoid it. So that discounts
<cfobject>
and<cfinvoke>
for consideration IMO.All of this is opinion though.
As I said,
entityNew()
is specifically designed for creating ORM objects, so there might be something to be said to using that in a mixed environment that has a mix of ORM-based and vanilla objects.As for
createObject()
and new? I now reservecreateObject()
for Java objects, and use new for CFML objects. For code clarity.Also bear in mind that new also calls the init() method (or whatever the initmethod attribute on the component suggests should be called).