DEV Community

Cover image for ColdFusion structKeyExists vs keyExists
James Moberg
James Moberg

Posted on

ColdFusion structKeyExists vs keyExists

We have a lot of logic that processes data and it extensively uses structKeyExists to determine whether or not data exists prior to performing additional logic. I've been modernizing my legacy ColdFusion code and am wondering if we should invest any time to converting structKeyExists() to keyExists(). Are there any benefits apart from readability? Any assumptions regarding which is more performant?

While I've read that looping over a function multiple times isn't the best way to compare performance, it often reveals issues... especially when one performs 2-4 times faster than the other.

Here's some quick stats comparing 2,000 built-in cfml function calls. (NOTE: It didn't matter which version of ACF2016+ or Lucee is used.)

function ns
structKeyExists 810,284
keyExists 1,477,436

Am I going about this wrong? Am I asking the right questions? Is there any expectation that the performance times would be better (or at least the same) when using keyExists()?

Source code

<!--- 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>

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay