Skip to main content
Ben Nadel at cf.Objective() 2014 (Bloomington, MN) with: Elliott Sprehn
Ben Nadel at cf.Objective() 2014 (Bloomington, MN) with: Elliott Sprehn

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