ColdFusion CreateObject() vs. Java NewInstance()

Posted October 8, 2006 at 12:55 PM by Ben Nadel

Tags: ColdFusion

When I tried doing Spike's Hello World class loader example, I came across a Java class method named NewInstance(). This method returns a new instance of the given class instantiated as if it the "new" directive was called with an empty list of constructor parameters. For those of you who use ColdFusion's CreateObject() method, you know that there is some overhead with creating Java objects. I wanted to see if it might be faster to store a class definition and then use NewInstance() than it would be to use CreateObject().

  • <!---
  • Get a string buffer class definition. This is the class
  • that will use later with NewInstance().
  • --->
  • <cfset objStringBufferClass = CreateObject(
  • "java",
  • "java.lang.StringBuffer"
  • ).GetClass()
  • />
  •  
  •  
  • <!--- Set the number of iterations. --->
  • <cfset intIterations = 1000 />
  •  
  •  
  • <!---
  • Now we want to test the traditional CreateObject
  • methodology where we use ColdFusion to create a new Java
  • object as we need it.
  • --->
  • <cftimer label="CreateObject Method" type="outline">
  •  
  • <!--- Loop over the number of iterations to test speed. --->
  • <cfloop
  • index="intI"
  • from="1"
  • to="#intIterations#"
  • step="1">
  •  
  • <!--- Create the string buffer. --->
  • <cfset objBuffer = CreateObject(
  • "java",
  • "java.lang.StringBuffer"
  • ).Init()
  • />
  •  
  • <!--- Add a random name to the text buffer. --->
  • <cfset objBuffer.Append(
  • ListGetAt(
  • "Libby,Ashely,Sarah",
  • RandRange( 1, 3 )
  • )
  • ) />
  •  
  • </cfloop>
  •  
  • </cftimer>
  •  
  •  
  • <!---
  • Now, we want to test the Java method NewInstance() as a
  • means to create new Java objects as we need them.
  • --->
  • <cftimer label="NewInstance Method" type="outline">
  •  
  • <!--- Loop over the number of iterations to test speed. --->
  • <cfloop
  • index="intI"
  • from="1"
  • to="#intIterations#"
  • step="1">
  •  
  • <!---
  • Create the string buffer using the NewInstance
  • method called on our stored StringBuffer class
  • definition.
  • --->
  • <cfset objBuffer = objStringBufferClass.NewInstance() />
  •  
  • <!--- Add a random name to the text buffer. --->
  • <cfset objBuffer.Append(
  • ListGetAt(
  • "Libby,Ashely,Sarah",
  • RandRange( 1, 3 )
  • )
  • ) />
  •  
  • </cfloop>
  •  
  • </cftimer>

The results were not very interesting. There was no huge performance gain. There's wasn't even any consistency. In small cases, such as 1,000 iterations, the NewInstance() method was faster than the CreateObject() method... but only by milliseconds. However, when we pumped the test case up to around 10,000 iterations, the CreateObject() method was consistently faster. Not sure that is all about.

So, while it may have been a neat thought, it seems that NewInstance() holds no advantage in any general Java class instantiation case. Plus, you can't pass in constructor arguments with it. Well, at least not with this version... it looks like in Java there is some sort of reflect Constructor class that has a NewInstance() method that can take arguments... but I don't feel like it's worth looking into at this point.



Reader Comments

Dec 20, 2010 at 5:53 AM // reply »
3 Comments

Thanks a lot Ben.It help me a lot for creating my first java object.


Dec 20, 2010 at 10:53 AM // reply »
10,640 Comments

@Sonu,

No problem. CreateObject() is a great and easy way to create Java objects. There's a whole world of power to be leveraged just below the surface of ColdFusion.


Feb 10, 2011 at 8:40 AM // reply »
1 Comments

There might be 2 things making that the test results are not faster for newInstance:

- the newInstance method is called on a java proxy. Which might wrap the new instance anyway, just like createobject.

- The random name adding could be slower then creating objects, therefore it could mask the speed differences between createobject and newinstance.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »