Skip to main content
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Doug Hughes and Ezra Parker and Dan Wilson and John Mason and Jason Dean and Luis Majano and Mark Mandel and Brian Kotek and Wil Genovese and Rob Brooks-Bilson and Andy Matthews and Simeon Bateman and Ray Camden and Chris Rockett and Joe Bernard and Dan Skaggs and Byron Raines and Barney Boisvert and Simon Free and Steve 'Cutter' Blades and Seth Bienek and Katie Bienek and Jeff Coughlin
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Doug Hughes Ezra Parker Dan Wilson John Mason Jason Dean Luis Majano Mark Mandel Brian Kotek Wil Genovese Rob Brooks-Bilson Andy Matthews Simeon Bateman Ray Camden Chris Rockett Joe Bernard Dan Skaggs Byron Raines Barney Boisvert Simon Free Steve 'Cutter' Blades Seth Bienek Katie Bienek Jeff Coughlin

ColdFusion JavaCast() Adds No Performance Hit

By
Published in

I love the fact that Java lives up ColdFusion's skirt. That is exciting. What's not as exciting is the fact that, in a seemingly random fashion, ColdFusion has trouble performing automatic casts from ColdFusion data types to Java data types. I have never really used JavaCast() to do this unless an actual error was being thrown. However, as I start to write more and more UDFs that harness Java, I have to get into the practice of doing a JavaCast() in every required place. This will make the inner working of the UDF are as black-boxed as possible.

I was concerned that JavaCast() method calls would be adding a lot of overhead to the page request. Each method call has on overhead to it. But, I am excited to say that after some basic speed testing, JavaCast() shows no apparent processing overhead. My tests were basic; I created a Java string, initialized it with a ColdFusion string (with and without a Cast) and then Upper-Cased the string:

<cftimer label="No Java Cast" type="outline">

	<!--- Loop 10,000 times. --->
	<cfloop index="intI" from="1" to="10000" step="1">

			<!--- Create a string based on date/time. --->
			<cfset strText = ("Now is " & Now()) />

			<!--- Create an upper cased string using Java. --->
			<cfset strUpperCase = CreateObject(
				"java",
				"java.lang.String"
				).Init( strText ).ToUpperCase() />

	</cfloop>

</cftimer>

<cftimer label="Java Cast" type="outline">

	<!--- Create a string based on date/time. --->
	<cfloop index="intI" from="1" to="10000" step="1">

		<!--- Create a string based on date/time. --->
		<cfset strText = ("Now is " & Now()) />

		<!--- Create an upper cased string using Java. --->
		<cfset strUpperCase = CreateObject(
			"java",
			"java.lang.String"
			).Init(
				JavaCast( "string", strText )
				).ToUpperCase() />

	</cfloop>

</cftimer>

Both of these tests performed anywhere from 450 ms to 550 ms. Neither of them showed any real trending towards better or worse.

Want to use code from this post? Check out the license.

Reader Comments

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel
Managed hosting services provided by:
xByte Cloud Logo