Which of these is more efficient in ColdFusion?
isDefined('url.myvar')
or
structKeyExists(url, 'myvar')
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
These days (CF8+) the difference in speed is not that great. However,
structKeyExistsis indeed a little faster. Here’s why.When you use
isDefined, the string you pass in is searched for as a key name in several scopes. As of CF9, the list of scopes, in the order checked is: (source)Even if you use the scope name with
isDefined(like:if isDefined('variables.foo')) the list will still be checked in order; and if the variablelocal.variables.foois defined, it will be found BEFOREvariables.foo.On the other hand,
structKeyExistsonly searches the structure you pass it for the existence of the key name; so there are far fewer places it will have to look.By using more explicit code (
structKeyExists), not only are you gaining some performance, but your code is more readable and maintainable, in my opinion.