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  |  Permalink  |  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
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »