ColdFusion:更高效的structKeyExists()而不是isDefined() [英] ColdFusion: More efficient structKeyExists() instead of isDefined()
问题描述
其中哪些在ColdFusion中更有效?
isDefined('url.myvar')
或
structKeyExists这些日子(CF8 +)是一个很好的解决方案,可以帮助我们解决这个问题。速度的差异不是很大。但是, structKeyExists
确实有点快。这是为什么。
当您使用 isDefined
时,传入的字符串将在几个范围。自CF9开始,范围列表(按检查顺序)为:(源)
- 本地(仅限本地功能,UDF和CFC)
- 参数
- 线程局部(仅内部线程)
- 查询(不是真正的范围,适用于查询循环中的变量)
- 变数
$ b >
- 表单
- Cookie
- 客户
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)
- Local (function local, UDFs and CFCs only)
- Arguments
- Thread local (inside threads only)
- Query (not a true scope, applies for variables within query loops)
- Thread
- Variables
- CGI
- CFFile
- URL
- Form
- Cookie
- 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屋!