Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active December 31, 2021 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamoCA/809c58aaff993cccd979c143f8de7698 to your computer and use it in GitHub Desktop.
Save JamoCA/809c58aaff993cccd979c143f8de7698 to your computer and use it in GitHub Desktop.
Attempt to compare performance of ColdFusion StructKeyExists() versus keyExists() (cfml)
<!--- 20211230 https://gist.github.com/JamoCA/809c58aaff993cccd979c143f8de7698 --->
<h2>Attempt to compare performance of StructKeyExists() versus keyExists()</h2>
<cfscript>
request.nanoTime = createObject("java", "java.lang.System");
numeric function getNano() output=false hint="returns nano time (more accurate)" {
return request.nanoTime.nanoTime();
}
results = [:];
s = {};
maxLoop = 2000;
t = getNano();
for (i=1; i <= maxLoop; i+=1) structKeyExists(s, "a");
results["#numberFormat(maxLoop)# StructKeyExists()"] = val(getNano()-t);
t = getNano();
for (i=1; i <= maxLoop; i+=1) s.keyExists("a");
results["#numberFormat(maxLoop)# keyExists()"] = val(getNano()-t);
t = getNano();
for (i=1; i <= maxLoop; i+=1) structKeyExists(s, "a");
results["#numberFormat(maxLoop)# StructKeyExists() [2nd]"] = val(getNano()-t);
writeOutput("<hr>");
t = getNano();
structKeyExists(s, "a");
results["1 StructKeyExists()"] = val(getNano()-t);
t = getNano();
s.keyExists("a");
results["1 keyExists()"] = val(getNano()-t);
writeoutput("<div><b>CF Version:</b> #SERVER.ColdFusion.ProductVersion#</div>");
writeDump(var=results, label="StructKeyExists vs keyExists");
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment