Use Java for beans in ColdFusion
I was excited to see that Adobe sped up the CreateObject(’component’) function in ColdFusion 8. Was I finally able to use beans without dramatically slowing down my page loads? The answer is no, but you should continue reading to see how I got to use beans in my ColdFusion project.
Technorati Tags:
coldfusion, java, web development, web, development, programming, cf, adobe, j2ee
ColdFusion components suck at beans
According to some Mach-II documentation by Hal Helms (PDF), if we had an arbitrary Address bean, this is how it should be written using a CFC:
<cfcomponent>
<cffunction name="init" access="public" returntype="Address">
<cfargument name="line1" type="string" required="false" default="">
<cfargument name="line2" type="string" required="false" default="">
<cfargument name="city" type="string" required="false" default="">
<cfargument name="state" type="string" required="false" default="">
<cfargument name="zip" type="string" required="false" default="">
<cfset this.setLine1(arguments.line1)>
<cfset this.setLine2(arguments.line2)>
<cfset this.setCity(arguments.city)>
<cfset this.setState(arguments.state)>
<cfset this.setZip(arguments.zip)>
</cffunction>
<cffunction name="setLine1" access="public" returntype="void">
<cfargument name="line1" type="string" required="true">
<cfset this.line1 = arguments.line1>
</cffunction>
<cffunction name="getLine1" access="public" returntype="string">
<cfreturn this.line1>
</cffunction>
<--- etc... --->
</cfcomponent>
I agree that we’d want to form our beans like this. The problem is that when you encapsulate a large record set of beans and put them in an array, you get a super-long load time.
Take, for example, a page I was creating for a sitemap that lists all products in the database. The page took over 11,000 ms to load. And we all know that it’s unacceptable for any web page to take 11 seconds to load!
Java to the rescue
Elliot Sprehn commented on Mark Drew’s blog about what really happens when you create a CFC:
A “simple bean” in CF is an entire page, with it’s own PageContext, metadata, output buffers. Each method is an entire class so we can pass method pointers around, something Java can’t do, and there’s lots of stuff in there to deal with the dynamic typing that Java wouldn’t be dealing with, and more importantly could optimize the hell out of in the compiler because it knows the types in advance.
So every time you go to add a CFC instance to your array, you’re instantiating several Java classes just to create that one instance. This adds extra work for your server.
I’ve fought with this in the past. But this time I decided to take that blog entry’s advice and implement the beans in Java.
The result: a 344% 34,400% increase in performance! If you ever needed the proof that certain tools are better suited for certain tasks, this would be a great example. I’ll continue to use CFCs to abstract database queries, write business logic, and serve up HTML (things that ColdFusion are great for), but I’ll be using Java classes for data encapsulation. Good on Macromedia giving us the power of Java to go along with CF!