ColdFusion CreateObject() vs. Java NewInstance()

Posted October 8, 2006 at 12:55 PM

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().

 Launch code in new window » Download code as text file »

  • <!---
  • 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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 14, 2010 at 2:01 PM
Creating UI Elements With Low-Coupling And Conditional Event Handling
@Ryan, It's definitely an interesting approach. I'm used to binding to objects / elements that the idea of binding to an event object is a bit much to wrap my head around just yet. As far as the TH ... read »
Mar 14, 2010 at 10:08 AM
The Magic Of Thinking Big By David Schwartz (Thanks Clark Valberg!)
@Kamal, You can get it off of iTunes / Audible.com. ... read »
Mar 13, 2010 at 9:14 PM
The Magic Of Thinking Big By David Schwartz (Thanks Clark Valberg!)
Ben, COuld you plz tell me how to find the audio book version of The Magic of Thinking Big by David J. Schwartz, Ph.D Thanks ... read »
sun
Mar 13, 2010 at 6:21 PM
Sending (SMS) Picture Messages With ColdFusion And CFMail
MY BAD! @tmomail.net IS correct ... but, ONLY from pic taken W/ a cell -- NOT as attachment in an email ... (took ~4 min. for it to arrive ... then would NOT DL to my cell) ... read »
sun
Mar 13, 2010 at 6:16 PM
Sending (SMS) Picture Messages With ColdFusion And CFMail
@tmomail.net is NOT correct! IS t-mobile addy ... but, ONLY for a txt msg! ... read »
Mar 12, 2010 at 8:24 PM
Creating UI Elements With Low-Coupling And Conditional Event Handling
@Ben, Yes, that's exactly what's happening. That's the approach YUI has taken with custom events, you subscribe the handler to the Event object itself. I really like the event system in YUI, it's ... read »
Mar 12, 2010 at 7:34 PM
FLV 404 Error On Windows 2003 Server
I spent a good couple of hours on this today. What a pain. On my old server, everything was fine. I migrated the site to a new server, and suddenly, all the files with audio didn't work. Just got ... read »
Mar 12, 2010 at 7:26 PM
Creating UI Elements With Low-Coupling And Conditional Event Handling
@Ryan, I finally got around to reading that YUI blog post you linked (it took me a fews months, but I did it). I wanted to ask you something (rather than on his only cause it is so old), but from w ... read »