22

Which of these is more efficient in ColdFusion?

isDefined('url.myvar')

or

structKeyExists(url, 'myvar')
Jeromy French
  • 11,812
  • 19
  • 76
  • 129
James T
  • 3,292
  • 8
  • 40
  • 70

2 Answers2

42

These days (CF8+) the difference in speed is not that great. However, structKeyExists is 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)

  1. Local (function local, UDFs and CFCs only)
  2. Arguments
  3. Thread local (inside threads only)
  4. Query (not a true scope, applies for variables within query loops)
  5. Thread
  6. Variables
  7. CGI
  8. CFFile
  9. URL
  10. Form
  11. Cookie
  12. Client

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 variable local.variables.foo is defined, it will be found BEFORE variables.foo.

On the other hand, structKeyExists only 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.

Adam Tuttle
  • 19,505
  • 17
  • 80
  • 113
  • Very good! It is interesting to see how it processes the functions differently. – James T Oct 18 '10 at 12:41
  • I also think ColdFusion needs to run an eval on the string you provide. StructKeyExists avoids a lot of work. – Aaron Greenlee Oct 18 '10 at 14:13
  • 2
    There's one big problem with `structKeyExists()` though. If you deserialize a JSON string with ColdFusion and you have a property `myVal` with the value `null`, then `structKeyExists(object, "myVal")` will return `true`, while `isDefined("object.myVal")` will not. Or in other words, if you try to access the property with `object.myVal` after checking with `structKeyExists()`, you'll get an error. – android Feb 18 '15 at 07:46
  • True. But that's a bug in the way CF handles null. Try `isNull()` before your `structKeyExists()` call, whenever null is an acceptable value. – Adam Tuttle Feb 18 '15 at 11:31
13

Use the one which is easier to read and best shows what you're doing.

The difference between the two is incredibly small, and very likely not worth worrying about at all.

Don't waste time optimising code unless you have a proven and repeatable test case which demonstrates the slowness.

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
  • 2
    +1 . It is definitely worthwhile to understand *how* the two functions operate. But unless you have a performance problem, writing readable code that behaves correctly is far more important IMO. – Leigh Oct 18 '10 at 16:53
  • I completely agree, but I am still a regular user of `structKeyExists`. I find that its explicit nature makes everything crystal clear, where `isDefined` does not always, which results in more time and headaches when coming back to old code (and/or someone else's code) months or years later. – Adam Tuttle Oct 18 '10 at 17:06
  • Though IsDefined() is more intuitively named, what you just described is still a better reason to choose one over the other,than premature concerns about performance. At least IMHO ;-) – Leigh 8 mins ago – Leigh Oct 18 '10 at 18:05
  • very late comment, but what i suggest it sometimes pages do time out due to this key issues, throws gateway errors – Regual Feb 08 '14 at 18:03