Reading time: 2 – 2 minutes
The graphics will show it, but basically I was comparing the performance between the two of them. And guess what?
In my tests on CF7, isDefined seems to be slightly faster than structKeyExists. The factor here is that isDefined() is super-fast when the scope exists, i.e. isDefined(“session.userID”), but really slow if a non-existent scope is used, e.g. isDefined(“unknownStruct.userID”).
However, in CF8 and Railo, structKeyExists() simply leaves isDefined() behind.
The code:
<!--- Number of iterations --->
<CFSET iterations=100000>
<!--- Looping using isDefined --->
<cfset tmp1 = getTickCount() />
<cfloop from="1" to="#iterations#" index="i">
<cfif isdefined("variables.blog")></cfif>
</cfloop>
<cfset tmp2 = getTickCount() />
<cfset total1 = tmp2 - tmp1 />
<!--- Looping using structKeyExists --->
<cfset tmp3 = getTickCount() />
<cfloop from="1" to="#iterations#" index="j">
<cfif structKeyExists(variables,"blog")></cfif>
</cfloop>
<cfset tmp4 = getTickCount() />
<cfset total2 = tmp4 - tmp3 />
<!--- The cfchart --->
<cfchart format="gif" xaxistitle="function" yaxistitle="Loading Time">
<cfchartseries type="bar" serieslabel="isDefined">
<cfchartdata item="isDefined" value="#variables.total1#">
</cfchartseries>
<cfchartseries type="bar" serieslabel="structKeyExists">
<cfchartdata item="structKeyExists" value="#variables.total2#">
</cfchartseries>
</cfchart>
And the results:

Ok evaluate() removed. You’re right, there’s no need to use evaluate in this case.
Can I ask why you’re using evaluate in your example? It’s completely unnecessary.
Marcos, is there a reason to revise your test here? I’m asking since it’s fairly old, and I’m not sure if anything has changed in the last couple of years.
Can I ask why you think that “IsDefined” is quicker than “StructKeyExists”. From my view of your chart, “isDefined” is approx 500ms, “StructKeyExists” takes around 50ms …
However, on my linux based CF8 server, “isDefined” takes 493ms, “StructKeyExists” takes 338ms …
This test happens to have been done using CF7 at the time. I’ve modified the text slightly, so it makes it more clear. Thanks for pointing that out.