Avg. Rating 5.0

Problem

Creating a large number of objects using CreateObject can be slow and degrade performance.

Solution

ColdFusion allows an application to create a typed object from a structure by specifying the __type__ key in the structure.

Detailed explanation

When creating a large number of typed objects in ColdFusion, the CreateObject method can slow down an application and degrade performance by a significant amount. 

Creating typed objects is usually done when working with Adobe Flex through remoting. 

In order to improve the perfomance of object creation it is possible to create a typed object from a structure by specifying the __type__ key and supplying the object type as the value.

The following code demonstrates both methods of creating the same object:

Using CreateObject:

<cfset oPerson = CreateObject( "component", "model.vo.PersonVO"
) />
<cfset oPerson['firstName'] = "Kevin" />
<cfset oPerson['lastName'] = "Schmidt" />

Using __type__

<cfset oPerson = StructNew() />
<cfset oPerson['__type__'] = "model.vo.PersonVO" />
<cfset oPerson['firstName'] = "Kevin" />
<cfset oPerson['lastName'] = "Schmidt" />

Both of the objects above will be of the same type, model.vo.PersonVO when they are returned.  While this may seem like a hack, it is officially supported by Adobe and appears in the ColdFusion documentation.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes