javascript 函数作用域、变量声明

发布于 2024-10-20 10:59:51 字数 1062 浏览 5 评论 0原文

假设我有以下场景:

var namespace = {};
(function($)
{
    $.extend(namespace, 
    {
        test1: function(someArray, someObj)
        {
             for(var i= 0, ii= someArray.length;i<ii;i++)
             {
                 var text = someObj[someArray[i]];
                 // do something with text
             }
        },
        test2: function(someArray, someObj,i,ii,text)
        /*
             see that the i,ii,text are  unused parameters,
             that will be used instead of variables
        */
        {
             for(i= 0, ii= someArray.length;i<ii;i++)
             {
                  text = someObj[someArray[i]];
                 // do something with text
             }
        },
    });
})(jQuery);

现在,test1 和 test2 的结果是相同的...但是性能、内存使用情况如何... 以上述两种方式声明 i,ii, 测试变量有什么区别吗?

例如,我认为 test2 可能更有效,因为变量位于本地函数作用域中,因此在函数退出后,执行上下文将被销毁,释放用于参数的资源......变量将不会被分配到全局对象“窗口”。

那么什么方法效果最好呢?为什么?


[编辑]

感谢大家的回答!

如果代码有可读性问题也没有问题...我现在只对性能/内存使用感兴趣。

lets say that I have the following scenario:

var namespace = {};
(function($)
{
    $.extend(namespace, 
    {
        test1: function(someArray, someObj)
        {
             for(var i= 0, ii= someArray.length;i<ii;i++)
             {
                 var text = someObj[someArray[i]];
                 // do something with text
             }
        },
        test2: function(someArray, someObj,i,ii,text)
        /*
             see that the i,ii,text are  unused parameters,
             that will be used instead of variables
        */
        {
             for(i= 0, ii= someArray.length;i<ii;i++)
             {
                  text = someObj[someArray[i]];
                 // do something with text
             }
        },
    });
})(jQuery);

Now, the result of the test1 and test2 are the same... but what about the performance, memory usage...
Is there any difference between declaring the i,ii, test variables in the two ways presented above ?

I think that the test2, for example, is probably more efficient because the variables are in the local function scope so after the function exits, the execution context is destroy, releasing the resources used for the arguments... the variables will not be assigned to the global object 'window'.

So what method is performing best? and why?


[Edit]

Thanks all for your answers !

There is no problem if the code has readability issues... I`m only interested now about the performance/memory usage.

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

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

发布评论

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

评论(5

疯到世界奔溃 2024-10-27 10:59:51

如果您不使用 var i 声明变量,那么它们将成为隐式全局变量。

始终声明您的变量。如果您对此进行任何基准测试,您会发现声明的局部变量实际上比隐含的全局变量更快。而且你也不会以这种方式泄漏到全局状态。

基准!

如您所见,性能是相同的。

就内存使用而言,局部变量 (test1) 可能更好,因为编译器不必记住该函数有 5 个参数。

但这是纳米优化,如果您关心这种能力的性能差异,请改为编写程序集。寻找可读可维护代码。

[编辑]

没有注意到方法参数中的“局部”变量。这是可读性杀手!不要那样做。您会发现 test1 可能仍然更高效。

If you do not declare your variables with var i then they become implicitly global.

Always declare your variables. If you did any benchmarking on that you would find that declared local variables are actually faster then implied global variables. Also you don't leak to the global state that way.

Benchmark!.

As you can see the performance is identical.

In terms of memory usage, local variables (test1) are probably better as the compiler doesn't have to remember that the function has 5 parameters.

But that's a nano optimisation If you care about performance differences of this caliber write assembly instead. Go for readable and maintanable code.

[Edit]

Didn't notice "local" variables in method parameter. That is a readability killer! Don't do that. You will find that test1 is probably still more efficient.

香草可樂 2024-10-27 10:59:51
  1. 你为什么不分析你的代码?
  2. 这些变量在 test1 中也是本地的。您使用 var i 声明它们。
    这些方法之间没有区别。
  1. Why don't you profile your code?
  2. The variables are also local in test1. You are declaring them with var i.
    There is no difference between these methods.
菩提树下叶撕阳。 2024-10-27 10:59:51

“test1”中的变量都是用 var 声明的,因此它们不是全局的。这两者本质上应该是一样的。

The variables in "test1" are all declared with var, so they're not global. Those two should be essentially the same.

人心善变 2024-10-27 10:59:51

test1 更快,因为每次 JavaScript 查找符号(例如变量名)时,它都会从本地范围开始查找。因此,通过使用全局范围,它必须在更多地方查找才能找到符号。这同样适用于参数,但它们比全局变量更好。

test1 is faster, because every time JavaScript looks for symbols (such as a variable name) it starts by looking in the local scope. So by using global scope it has to look in more places to find the symbols. The same applies for parameters, but they are better than globals.

星軌x 2024-10-27 10:59:51

它可能很小,但我相信在每次迭代中声明一个新变量(文本)将需要新的内存分配。虽然我不确定 javascript 如何处理这个问题。我通常会事先声明变量,然后出于这个原因分配值,但这只是因为有人说“嘿,你应该这样做”并提出了相同的论点。

It may be miniscule, but declaring a new variable (text) in every iteration would require new memory allocation I believe. Though I'm not sure how javascript handles that. I usually declare variables beforehand and then assign values afterwards for that reason, but that is only because someone said "hey you should do it that way" and presented the same argument.

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