Javascript 使用显式 self/window 对象来提高性能

发布于 2024-09-17 01:31:29 字数 790 浏览 12 评论 0原文

我在 MSDN 上读到要改进的内容脚本效率,您可以使用 self 使隐式窗口引用显式化。

  1. 你知道这是真的吗?这是否基本上意味着调用 self.location 比调用之前没有 window 对象的简单 location 更有效?

  2. 由于 MSDN 文本提到的是 self 而不是 window,因此这种性能提升是否仅在使用 self 时才会发生? 根据 此处 windowselfwindow.self 是同一件事,所以我们使用什么并不重要,我只是想确认一下。

  3. 此外,按照 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.

  1. Do you know if this is true? Does this basically mean that for instance calling self.location is somewhay more efficient than calling simply location with no window opject before?

  2. Since the MSDN text is referred to the self and not window, does this performance increase happens only using self?
    According to here window and self and window.self are the same thing, so it shouldn't matter what we use, i'm asking only to make sure.

  3. Moreover following what stated in MSDN calling window.self should be somehow more performant than calling self because this last one is a property of window so by calling window.self we use an explicit ref.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那些过往 2024-09-24 01:31:29

这种微优化实际上完全是浪费时间,但值得一提的是,常见的习惯用法是这样编写 Javascript:

(function(window, undefined) {
  // your code, thousands of lines of sheer beauty
})(this);

这为您提供了对“窗口”的本地引用,以及一个可靠的“未定义”变量启动。

为什么这是浪费时间?因为对于任何普通代码,您所说的执行时间最多会减少一两毫秒。没有人会注意到这一点。确保您用于执行编码的实际算法是合适的,并让 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:

(function(window, undefined) {
  // your code, thousands of lines of sheer beauty
})(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.

疯了 2024-09-24 01:31:29

尽管这在很大程度上是一种微观优化,但直接属性引用总是比变量查找更快。当您编写 location 时,会执行类似以下操作:

  1. 查找在当前作用域中声明的 location,如果找到则返回并退出。
  2. 将范围层次结构向上一级。
  3. 如果作用域不是全局,则转到1。如果作用域是全局,则检查全局作用域中的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:

  1. Look for location declared in the current scope, return and exit if found.
  2. Go up the scope hierarchy by one.
  3. If scope is not Global, go to 1. If scope is Global, check for 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 for self, which is also a property of window. self is a reference to window, so window.location should be faster than window.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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文