JavaScript的对象成员访问 [英] Javascripts Object member access
问题描述
我想了解一下关于对象成员访问时间。更多precisely,我在窗口
对象标杆访问时间。我相信我能在这里解释一下我自己最的行为,但我感觉更好听一些意见。
I'm wondering about access times on object members. More precisely, I'm benchmarking access times on the window
object. I believe I can explain most behavior here on my own, but I'd feel better to hear some comments.
用例:在不同的propertys不同的访问时间
Usecase: Different access times on different propertys ?
我标杆在Firefox 3.6.8(窗口),简单的测量code如下:
I'm benchmarking on Firefox 3.6.8 (windows), the simple measuring code looks like:
var loop = 100000;
console.time('bench');
while(loop--){
if(window.JSON)
var foo = 0;
}
console.timeEnd('bench');
第一个奇怪的是,它使一个不同的我找哪个属性了。例如, window.JSON
似乎更快地访问,比 window.localStorage
。有哪些可以得到更快的访问其他propertys /方法。结果
由于没有规范或认定中的 ECMA-262语言规范,它为了密钥必须有一个对象,我想每个浏览器厂商实现它自己的逻辑的次序键
存储在内存中。结果
难道这是对这种行为的解释?像 JSON
是第一项,并将位置
更是在结束了吗? (在我的测试环境至少)
The first strange thing is, it makes a different which property I'm looking up. For instance, window.JSON
seems to be faster to access, than window.localStorage
. There are other propertys/methods which can get accessed even faster.
Since there is no specification or defination within in the the ECMA-262 Language Specification, which order keys must have in an object, I guess each browser vendor implements it's own logic in which order keys
are stored in memory.
Could this be an explanation for this behavior? Like, JSON
is one of the very first keys and location
is more at the end? (In my test environment at least)
-
另一件事我被注意,在调用如果(JSON)
略快于如果(window.JSON)
。如果我们忘记了,你总是应该做的,因为可能的引用错误的第二个电话,这些电话应该有相同的访问时间。我所知道的ECMAscripts行为对嵌套成员查找
(嵌套成员将导致Javascript引擎要经过对象成员分辨率,每次遇到一个圆点),从而 window.location.href
必须大于 location.href
慢,但是在这种情况下, ..有之间的差异 JSON
和 window.JSON
?
Another thing I noticed is, that calling if(JSON)
is slightly faster than if(window.JSON)
. If we forget about that you always should do the second call because of possible reference errors, those calls should have the same access time. I'm aware of ECMAscripts behavior on nested member lookups
(A nested member will cause the Javascript engine to go through the object member resolution, each time a dot is encountered), so window.location.href
must be slower than location.href
, but in this case.. is there a difference between JSON
and window.JSON
?
要结束这个,最快的办法知道窗口
对象是否拥有特定属性/方法是使用在
运营商。即快约10倍于上述实施例。
To end this, the fastest way to know whether or not the window
object owns a specific property/method is to use the IN
operator. That is about 10 times faster for the above examples.
推荐答案
您注意到可能需要做如何的的主机对象的由浏览器来实现。
The first strange thing you notice might have to do about how host objects are implemented by the browser.
的localStorage
是主机对象的,由环境提供, JSON
的另一方面,是的内置对象的通过ECMAScript中提供。
localStorage
is a host object, provided by the environment, JSON
on the other hand, is a built-in object provided by ECMAScript.
试图解决另一个内置的,你会得到更多或更少比具有相同效果 JSON
。
Try to resolve another built-in's and you will get more or less the same results than with JSON
.
现在,约 JSON
VS的区别 window.JSON
:
Now, the difference about JSON
vs window.JSON
:
窗口
只是一个全局对象指向自己,当你访问窗口
标识符上的属性名称解析过程发生的时间找到它。
window
is just a property on the global object that points to itself, when you access the window
identifier the name resolution process takes place to find it.
基本上引用:
JSON;
只涉及一个标识符查找(作用域链)和:
Involves only one identifier lookup (in the scope chain), and:
window.JSON;
涉及一个标识符查找(窗口
)和属性查找( window.JSON
)。
这篇关于JavaScript的对象成员访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!