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

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

问题描述

在 ColdFusion 中哪一个更有效?

Which of these is more efficient in ColdFusion?

isDefined('url.myvar')

structKeyExists(url, 'myvar')

推荐答案

这些天(CF8+)速度上的差异不是那么很大.不过,structKeyExists 确实快了一点.原因如下.

These days (CF8+) the difference in speed is not that great. However, structKeyExists is indeed a little faster. Here's why.

当您使用 isDefined 时,您传入的字符串会在多个范围内作为键名进行搜索.从 CF9 开始,范围列表按检查顺序为:(来源)

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. 本地(仅限本地函数、UDF 和 CFC)
  2. 参数
  3. 线程本地(仅限线程内部)
  4. 查询(不是真正的范围,适用于查询循环中的变量)
  5. 线程
  6. 变量
  7. CGI
  8. CF 文件
  9. 网址
  10. 表格
  11. Cookie
  12. 客户

即使您将范围名称与 isDefined 一起使用(例如:if isDefined('variables.foo')),仍然会按顺序检查列表;如果定义了变量local.variables.foo,则会在BEFORE找到variables.foo.

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.

另一方面,structKeyExists 仅搜索您传递给它的结构中是否存在键名;所以它需要看的地方要少得多.

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.

在我看来,通过使用更明确的代码 (structKeyExists),您不仅获得了一些性能,而且您的代码更具可读性和可维护性.

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天全站免登陆