HJCotton.net

December 12, 2009

CF9 Ternary Operator

by @ 11:22 pm. Filed under Coldfusion.

Coldfusion 9 finally has a ternary operator. It’s certainly a lot prettier to use than an IIF(), and you won’t have to deal with those odd occasions where you need to use DE() to get things to work. Here’s an example of how it works in CF9 and the equivalent using an IIF() and a standard “if” (keep in mind that the loops are to demonstrate performance only – they are otherwise quite out-of-place):

<cfparam name="URL.name">
<cfparam name="URL.isLight">
 
<cfset startTernary = GetTickCount()>
<cfloop from="1" to="10000" index="i">
	<cfset beerName = !URL.isLight? URL.name & " is a good beer" : "No light beers in this list!">
</cfloop>
<cfset endTernary = GetTickCount()>
 
<cfset startIIF = GetTickCount()>
<cfloop from="1" to="10000" index="i">
	<cfset beerName = IIF(!URL.isLight, "'#URL.name# is a good beer'", "'No light beers in this list!'")>
</cfloop>
<cfset endIIF = GetTickCount()>
 
<cfset startIf = GetTickCount()>
<cfloop from="1" to="10000" index="i">
	<cfif URL.isLight>
		<cfset beerName = "No light beers in this list!">
	<cfelse>
		<cfset beerName = "#URL.name# is a good beer">
	</cfif>
</cfloop>
<cfset endIf = GetTickCount()>
 
<cfoutput>
	Ternary: #endTernary - startTernary#ms<br />
	IIF: #endIIF - startIIF#ms<br />
	If: #endIf - startIf#ms<br />
</cfoutput>

Even over 10,000 iterations, the ternary option is head-to-head with a standard if statement, and only slightly faster than an IIF:
Ternary: 62ms
IIF: 77ms
If: 62ms

I’ve always heard that IIFs were slow, but I’ve always found them so handy that I overlooked whatever the performance consequences might be. While noticeable in these tests, it’s not something that would affect most projects at all. These results are also backed up in this post by Ben Nadel.

I won’t be using CF9 in a production setting in the immediate future, so until then it’ll still be IIFs for me. Still, some of these new language improvements and features are definitely worth looking into (even if they should have been added long ago!).

WITF Car Donation

by @ 7:01 pm. Filed under Humorous.

Dear Hadyn,

Thanks so much for recently donating your car to public broadcasting through the Car Talk vehicle donation program. Your thoughtfulness resulted in a net gift of $35.00 to WITF.

Excellent! Almost enough for a case of good beer… but not anywhere near what the sticker suggests:

HJ Chevrolet Free2.jpg

HJCotton.net

pages:

categories:

search:

archives:

December 2009
S M T W T F S
« Nov   Mar »
 12345
6789101112
13141516171819
20212223242526
2728293031  

So it goes.
— Kurt Vonnegut

general links:

IMG_4483.JPG

IMG_4483.JPG

01-23-2010

01-23-2010

other:

Page 1 of 1First

19 queries. 0.898 seconds