Coldfusion: is a default set, or is isdefined faster for arguments?

Is it faster to test the existence of isdefined("arguments.argument") or set the default value for arguments.argument ?

+4
source share
3 answers

I conducted a test using isDefined() , structKeyExists() and with the default value set. I figured it out

 isDefined - 184ms for 100k iterations structKeyExists - 149 ms for 100k iterations Default - 139ms for 100k iterations 

So, it seems that the default installation is the fastest option, but the difference between the default and structKeyExists so minimal that it doesn't matter. I would refrain from using isDefined() anywhere in your code.

The code I ran is below.

 <cfset loops = 100000> <cffunction name="myfunc" returntype="void"> <cfargument name="myTest"> <cfif isDefined('arguments.myTest')> </cfif> </cffunction> <cffunction name="myfunc2" returntype="void"> <cfargument name="myTest"> <cfif structKeyExists(arguments,'myTest')> </cfif> </cffunction> <cffunction name="myfunc3" returntype="void"> <cfargument name="myTest" default=""> </cffunction> <cfset start = getTickCount()> <cfoutput> <cfloop from="1" to="#loops#" index="i"> #myfunc()# </cfloop> </cfoutput> <cfdump var="#getTickCount() - start#"><br> <cfset start = getTickCount()> <cfoutput> <cfloop from="1" to="#loops#" index="i"> #myfunc2()# </cfloop> </cfoutput> <cfdump var="#getTickCount() - start#"><br> <cfset start = getTickCount()> <cfoutput> <cfloop from="1" to="#loops#" index="i"> #myfunc3()# </cfloop> </cfoutput> <cfdump var="#getTickCount() - start#"> 
+8
source

As @ matt-busche points out, worrying about this kind of performance is a case of premature optimization: performance differences are not significant. It probably took more time to type the question than during the cumulative time that one or the other will save you throughout the life of the application that you write.

What you should aim for is to write clear code that most accurately reflects the intent of the logic and the intended use of the code.

You should set a default value for your argument if that default value is the most likely useful value for this argument. This is not always appropriate: sometimes there is no “most likely useful value”, so the argument should not have a default value and, accordingly, requires that the calling code pass a value.

You cannot write code that sets the default value, so that subsequent code is not interrupted (for example: the default string argument is "", so you can assume that it exists in the following code).

One of the advantages of specifying a default argument is that the default value is reflected in the metadata of the component and its auto-generated documents. This is useful if you are writing an API for third-party consumption.

On the other hand, isDefined() should usually be avoided, since it is a rather limited, inaccurate function, and I saw how it gave false positives in some rare situations (and this was not only I did not understand the framework -).

Almost always you need to use structKeyExists() over isDefined() .

+6
source

In conventional coding, there is no tangible performance advantage between using one or the other.

To say, you should use structKeyExists() instead of isDefined() for one simple reason.

The structKeyExists() function forces you to view your variables. The isDefined() function allows you to use super-sloppy code, which leads to poor application performance.

ColdFusion: more efficient structKeyExists () instead of isDefined ()

+5
source

Source: https://habr.com/ru/post/1482226/


All Articles