ColdFusion:更高效的structKeyExists()而不是isDefined() [英] ColdFusion: More efficient structKeyExists() instead of isDefined()

查看:230
本文介绍了ColdFusion:更高效的structKeyExists()而不是isDefined()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中哪些在ColdFusion中更有效?

  isDefined('url.myvar')

  structKeyExists这些日子(CF8 +)是一个很好的解决方案,可以帮助我们解决这个问题。速度的差异不是很大。但是, structKeyExists 确实有点快。这是为什么。



当您使用 isDefined 时,传入的字符串将在几个范围。自CF9开始,范围列表(按检查顺序)为:(


  1. 本地(仅限本地功能,UDF和CFC)

  2. 参数

  3. 线程局部(仅内部线程)

  4. 查询(不是真正的范围,适用于查询循环中的变量)


  5. 变数
  6. $ b >
  7. 表单

  8. Cookie

  9. 客户


b $ b

即使你使用范围名称 isDefined (如: if isDefined('variables.foo')),列表仍将按顺序检查;如果定义变量 local.variables.foo ,将会找到 variables.foo



另一方面, structKeyExists 只会搜索您传递的结构,键名称;



通过使用更明确的代码( structKeyExists ),而不是只有你获得一些性能,但我的代码更可读和可维护,在我看来。


Which of these is more efficient in ColdFusion?

isDefined('url.myvar')

or

structKeyExists(url, 'myvar')

解决方案

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.

这篇关于ColdFusion:更高效的structKeyExists()而不是isDefined()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆