Ian Lurie // Apr 29 2010
OK, getting VERY geeky here. You only want to read this post if:
If you’re still here, this is a great tip for speeding up ColdFusion applications that handle really large strings:
Usually, in CF, you’d combine (concatenate) strings like this:
<CFSET string1 = “I”>
<CFSET string2 = ” am”>
<CFSET string3 = ” the nerdman!!! “>
<CFSET string1 = string1 & string2 & string3 & “mwahaha”>
Then this code:
would give you:
I am the nerdman!!! mwahahaha
But, if your strings get up to, say, 20,000 lines, this grinds to a halt. String concatenation can take 10 minutes instead of 10 seconds.
That’s ColdFusion’s way of telling you, “Enough already! Find a better way to put these variables together!”
And there is a better way.
ColdFusion is built on Java. So you can call some Java functions from within CF. Switch out the usual CFSET statements for a bit of Java code:
<cfset string1 = themap.concat(string1).concat(string2).concat(string3).concat(“mwahahaha”))>
concat is a Java string function. You can read about it until your eyes bleed here.
And the performance improvements will shock the hell out of you.
I wrote an XML sitemap generator to go with Portent’s crawler. It worked brilliantly for 2000-3000 URLs. In about 10 seconds you’d have a nice, basic XML sitemap ready for deployment.
But we have some sites with hundreds of thousands of URLs. Any attempt to process a URL list that large using conventional ColdFusion failed. “Failed” is a bit of an understatement, actually. “Made our servers crank their fans up to maximum, caused lights across the globe to dim and nearly melted our server rack to slag” is a better description. The application would run and run, and even after 3-5 minutes, no result.
So, I used the concat function, plus CFSAVECONTENT.
The application now processes 120,000 URLs in under 10 seconds.
So this isn’t some tiny little performance tweak. This is a huge performance improvement that you can make with a couple of lines of code. ColdFusion geeks, rejoice!
Ian Lurie is founder and CEO of Portent Inc., an internet marketing agency that has provided internet marketing, including PPC, SEO, social and analytics services, since 1995. Read More