Skip to content

Instantly share code, notes, and snippets.

@howellcc
Created June 5, 2015 12:31
Show Gist options
  • Save howellcc/bc5dc9c157e63d38da83 to your computer and use it in GitHub Desktop.
Save howellcc/bc5dc9c157e63d38da83 to your computer and use it in GitHub Desktop.
isDefined vs structKeyExists Performance Comparison
<cfsetting showdebugoutput="false" >
<cfset varName = "testVar#randRange(1,100)#">
<cfset evaluate("url.#varName# = 42") />
<cfset iterations = 100000>
<!--- test when variable is Defined --->
<cfset tickBegin = getTickCount()>
<cfloop from="1" to="#iterations#" index="i">
<cfset idNoScope = isdefined(varName)>
</cfloop>
<cfset isDefinedNoScope = getTickCount() - tickBegin />
<cfset tickBegin = getTickCount()>
<cfloop from="1" to="#iterations#" index="i">
<cfset idWithScope = isdefined("url.#varName#")>
</cfloop>
<cfset isDefinedWithScope = getTickCount() - tickBegin />
<cfset tickBegin = getTickCount()>
<cfloop from="1" to="#iterations#" index="i">
<cfset ske = structKeyExists(url,varName)>
</cfloop>
<cfset structkey = getTickCount() - tickBegin />
<!--- test when variable is not defined. --->
<cfset tickBegin = getTickCount()>
<cfloop from="1" to="#iterations#" index="i">
<cfset ndidNoScope = isdefined('notDefined')>
</cfloop>
<cfset ndisDefinedNoScope = getTickCount() - tickBegin />
<cfset tickBegin = getTickCount()>
<cfloop from="1" to="#iterations#" index="i">
<cfset ndidWithScope = isdefined("url.notDefined")>
</cfloop>
<cfset ndisDefinedWithScope = getTickCount() - tickBegin />
<cfset tickBegin = getTickCount()>
<cfloop from="1" to="#iterations#" index="i">
<cfset ndske = structKeyExists(url,"notDefined")>
</cfloop>
<cfset ndstructkey = getTickCount() - tickBegin />
<html><body><cfoutput>
<h1>Performance of isDefined vs. StructKeyExists over #iterations# iterations</h1>
<h3>Shallow Test: url.#varName#</h3>
<p>
<strong>Variable url.#varName# is Defined</strong><br/>
IsDefined Without Scope: #isDefinedNoScope#ms<br/>
IsDefined With Scope: #isDefinedWithScope#ms <br/>
StructKeyExists: #structKey#ms
</p>
<p>
<strong>Variable url.notDefined is NOT Defined</strong><br/>
IsDefined Without Scope: #ndisDefinedNoScope#ms<br/>
IsDefined With Scope: #ndisDefinedWithScope#ms <br/>
StructKeyExists: #ndstructKey#ms
</p>
<hr>
<!--- Deep Structure test. --->
<cfset a.much.deeper.structure = {}>
<cfset a.much.deeper.structure[varName] = 43>
<!--- test when variable is Defined --->
<cfset tickBegin = getTickCount()>
<cfloop from="1" to="#iterations#" index="i">
<cfset idWithScope = isdefined("a.much.deeper.structure.#varName#")>
</cfloop>
<cfset isDefinedWithScope = getTickCount() - tickBegin />
<cfset tickBegin = getTickCount()>
<cfloop from="1" to="#iterations#" index="i">
<cfset ske = structKeyExists(a.much.deeper.structure,varName)>
</cfloop>
<cfset structkey = getTickCount() - tickBegin />
<!--- test when variable is not defined. --->
<cfset tickBegin = getTickCount()>
<cfloop from="1" to="#iterations#" index="i">
<cfset ndidWithScope = isdefined("a.much.deeper.structure.notDefined")>
</cfloop>
<cfset ndisDefinedWithScope = getTickCount() - tickBegin />
<cfset tickBegin = getTickCount()>
<cfloop from="1" to="#iterations#" index="i">
<cfset ndske = structKeyExists(a.much.deeper.structure,"notDefined")>
</cfloop>
<cfset ndstructkey = getTickCount() - tickBegin />
<h3>Deep Structure Test: a.much.deeper.structure.#varName#</h3>
<p>
<strong>Variable a.much.deeper.structure.#varName# is Defined</strong><br/>
IsDefined With Scope: #isDefinedWithScope#ms <br/>
StructKeyExists: #structKey#ms
</p>
<p>
<strong>Variable a.much.deeper.structure.notDefined is NOT Defined</strong><br/>
IsDefined With Scope: #ndisDefinedWithScope#ms <br/>
StructKeyExists: #ndstructKey#ms
</p>
<hr>
<br/>
<cfset timingLoopString = '<cfset tickBegin = getTickCount()>
<cfloop from="1" to="#iterations#" index="i">
<cfset idNoScope = isdefined(varName)>
</cfloop>
<cfset isDefinedNoScope = getTickCount() - tickBegin />'>
<p>The timing loop for each of these tests looks like this:</p>
<pre>#xmlformat(timingLoopString)#</pre>
</cfoutput></body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment