ColdFusion JavaCast() Adds No Performance Hit
Posted August 3, 2006 at 4:52 PM by Ben Nadel
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.



