ColdFusion JavaCast() Adds No Performance Hit

Posted August 3, 2006 at 4:52 PM

Tags: ColdFusion

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:

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

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

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 19, 2010 at 12:14 PM
Why NULL Values Should Not Be Used in a Database Unless Required
@Eric, I think we should just agree that there are no cross-the-board rules on this. NULL values are good when they add value. That is highly contextual - there's nothing about NULL values that is ... read »
Mar 19, 2010 at 12:12 PM
Why NULL Values Should Not Be Used in a Database Unless Required
@Eric, By all means if you need referential integrity and foreign keys for things like "middle name", then you should use NULL values. In the applications I build, 99% of the time, I there is no ... read »
Mar 19, 2010 at 12:10 PM
Why NULL Values Should Not Be Used in a Database Unless Required
@Ben Nadel, Are you saying that correct data doesn't have any business value? Nonsense. What about referential integrity and foreign keys? ... read »
Mar 19, 2010 at 12:00 PM
Using jQuery To Leverage The OnChange Method Of Inputs
Thnx. FYI spelling error in your comment "// Add dirtry flag to the input in" ... read »
Mar 19, 2010 at 10:57 AM
Javascript Number.toFixed() Method
It doesn't have a base() method, but it can be put together simply with: Math.base = function(n, to, from) { return parseInt(n, from || 10).toString(to); }; ... read »
Mar 19, 2010 at 10:21 AM
ColdFusion Query of Queries Unexpected Data Type Conversion
@Hiren, Nice use of cast. ... read »
Mar 19, 2010 at 10:17 AM
Why NULL Values Should Not Be Used in a Database Unless Required
@Mike, If you enjoy using NULL values, then go for it; from a technical standpoint, there's certainly no reason to not use them if you like them. All I'm saying is that I think a lot of times the ... read »
Mar 19, 2010 at 10:12 AM
ColdFusion Path Usage And Manipulation Overview
@Sean, Did you ever figure out the "/" issue? Sorry I had no more ideas on that problem. ... read »