Javascript 使用显式 self/window 对象来提高性能
我在 MSDN 上读到要改进的内容脚本效率,您可以使用 self 使隐式窗口引用显式化。
你知道这是真的吗?这是否基本上意味着调用
self.location
比调用之前没有window
对象的简单location
更有效?由于 MSDN 文本提到的是
self
而不是window
,因此这种性能提升是否仅在使用self
时才会发生? 根据 此处window
和self
和window.self
是同一件事,所以我们使用什么并不重要,我只是想确认一下。此外,按照 MSDN 中所述,调用
window.self
应该比调用self
性能更高,因为最后一个是window
的属性> 因此,通过调用window.self
,我们使用显式引用。
谢谢
I read on MSDN that to improve scripting efficiency, you can use self
to make implicit window references explicit.
Do you know if this is true? Does this basically mean that for instance calling
self.location
is somewhay more efficient than calling simplylocation
with nowindow
opject before?Since the MSDN text is referred to the
self
and notwindow
, does this performance increase happens only usingself
?
According to herewindow
andself
andwindow.self
are the same thing, so it shouldn't matter what we use, i'm asking only to make sure.Moreover following what stated in MSDN calling
window.self
should be somehow more performant than callingself
because this last one is a property ofwindow
so by callingwindow.self
we use an explicit ref.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这种微优化实际上完全是浪费时间,但值得一提的是,常见的习惯用法是这样编写 Javascript:
这为您提供了对“窗口”的本地引用,以及一个可靠的“未定义”变量启动。
为什么这是浪费时间?因为对于任何普通代码,您所说的执行时间最多会减少一两毫秒。没有人会注意到这一点。确保您用于执行编码的实际算法是合适的,并让 Javascript 解释器/JIT 开发人员为您缩短这些毫秒。如果您沉迷于这样的事情,您将来很可能会得到运行速度较慢的代码,因为您会做一些奇怪的事情,而这些事情最终不会被解释器优化。
This is the kind of micro-optimization that's really a complete waste of time, but for what it's worth a common idiom is to write Javascript like this:
That gives you a local reference to "window", and a reliable "undefined" variable to boot.
Why is it a waste of time? Because for any ordinary code, you're talking about at most a millisecond or two shaved off the execution time. Nobody's ever going to notice that. Make sure that the actual algorithms you're using to do whatever it is you're coding are appropriate, and let the Javascript interpreter/JIT developers shave off those milliseconds for you. If you get obsessed with things like this, you're liable to end up with code that runs slower in the future because you'll have done weird things that end up not being optimized by the interpreter.
尽管这在很大程度上是一种微观优化,但直接属性引用总是比变量查找更快。当您编写
location
时,会执行类似以下操作:location
,如果找到则返回并退出。location
,如果找到则返回,否则抛出未声明的变量错误。类似的案例是 反对使用
with
语句 为对象属性创建范围。self
也是如此,它也是window
的一个属性。self
是对window
的引用,因此window.location
应该比window.self.location
更快。另外,请记住,实现可能会有所不同,因此您的情况可能会因浏览器的不同而有所不同。正如 Pointy “指出”的那样,大多数开发人员不必担心这样的微优化。差异为微秒,最终用户完全察觉不到。
进一步阅读:
Although it's very much a micro-optimization, direct property references are always faster than variable lookups. When you write
location
, something like the following is performed:location
declared in the current scope, return and exit if found.location
in global scope and return if found, otherwise throw undeclared variable error.A similar case is made against using the
with
statement to create a scope for object properties. The same goes forself
, which is also a property ofwindow
.self
is a reference towindow
, sowindow.location
should be faster thanwindow.self.location
. Also, remember that implementations can be different, so your mileage may vary from browser to browser.As Pointy "pointied" out, most developers don't have to worry about micro-optimizations like this. The difference is micro-seconds and completely unnoticeable to end users.
Further reading: