Javascript性能与关闭 [英] Javascript performance with closure

查看:124
本文介绍了Javascript性能与关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var name = function(n) {
    var digits = ['one','two','three','four'];
    return digits[n];
}

var namenew = (function() {
    digits = ['one','two','three','four'];
    return function(n) {
        return digits[n];
    }
}());

两个版本导致相同的输出,但是据说第二个版本比第一个版本。

Both the versions result in the same output, however it is said that the second version is much faster than the first version.

根据我的理解,第一个版本每次执行函数,第二个版本存储执行结果。这就是让我困惑作为一个功能/常规OOPS程序员。

As I understand, first version executes the function everytime where as the second version stores the result of execution. That is what confuses me as a functional/regular OOPS programmer.

如何使用内部上下文保存函数?发动机罩下发生了什么?有人可以澄清吗?

How can one save a function with its inner context? What is happening under the hood? Can some one pls clarify?

推荐答案

这个问题的真正答案大约有3页。但我尽量使它尽可能短。 ECMA- / Javascript都是关于执行上下文对象。在ECMAscript中有三种基本类型的上下文:全局上下文函数上下文

The real answer to that question would be about 3 pages long. But I try to make it as short as possible. ECMA-/Javascript is all about Execution Contexts and Object. There are three basic types of Context in ECMAscript: Global context, Function contexts and eval contexts.

每次调用函数时,引擎都会在它自己的 $ c>。此外,还有一个创建的激活对象。这个神秘的对象是函数上下文的一部分,它至少包含:

Every time you call a function, your engine will spawn it in it's own function context. Also, there is a such called Activation object created. This mystic object is part of a function context which consists out of at least:


  • [[Scope chain]]

  • 激活对象

  • this上下文值

在不同的引擎上可能有更多的属性,但这三个是ES的任何实现所必需的。但是,回到主题。如果调用函数上下文,则所有父上下文(或更精确地说,来自父上下文的 Activation对象)复制到 [[Scope]] 属性中。你可以把这个属性想象为一个数组,它包含(Activation-)对象。现在,任何与功能相关的信息都存储在激活对象(形式参数,变量,函数声明)中。

There may be more properties on different engines, but these three are required for any implementation of ES. However, back to the topic. If a function context is invoked, all parent contexts (or more precisly, the Activation objects from the parent contexts) are copied over into the [[Scope]] property. You can think of this property like an array, which holds (Activation-) Objects. Now, any function related information is stored in the Activation object (formal parameters, variables, function declarations).

在您的示例中, digits 变量存储在 namenew 。第二个当创建内部匿名函数时,它将激活对象添加到其 [[Scope]] 属性中。当您调用 digits [n] 时,Javascript会首先尝试在其自己的激活对象中找到该变量。如果失败,搜索进入Scopechain。瞧,我们找到了变量,因为我们从外部函数复制了AO。

In your example, the digits variable is stored in the Activation object for namenew. The second when the inner anonymous function is created, it adds that Activation object into its [[Scope]] propertys. When you call digits[n] there, Javascript first tries to find that variable in its own Activation object. If that fails, the search goes into the Scopechain. And voila, there we found the variable because we copied the AO from the outer function.

我已经为一个简短的答案写了太多,但真的给了一个好的答案对这样的问题,你必须解释一些关于ES的基本知识在这里。我想这只是足够给你一个想法是什么真正发生在引擎盖下(有很多更多的知道,如果你想阅读更多我会给你一些参考)。

I already wrote too much for a short answer, but to really give a good answer to such a question you have to explain some basic knowledge about ES here. I guess that is just enough to give you an idea what really happens "under the hood" (there is a lot more to know, if you want to read more I'll give you some references).

您要求它,您会得到:

http://dmitrysoshnikov.com/ecmascript/javascript-the-core/

这篇关于Javascript性能与关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆