javascript函数和参数对象,是否涉及成本 [英] javascript functions and arguments object, is there a cost involved

查看:57
本文介绍了javascript函数和参数对象,是否涉及成本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网络和框架中看到类似的代码是很常见的:

It is common place to see code like that around the web and in frameworks:

var args = Array.prototype.slice.call(arguments);

这样做,你转换参数 Object 进入一个真正的数组(尽管JS有真正的数组),它允许你将Array原型中的任何数组方法应用于它等等。

In doing so, you convert the arguments Object into a real Array (as much as JS has real arrays anyway) and it allows for whatever array methods you have in your Array prototypes to be applied to it, etc etc.

我记得在某处读取参数直接对象可能比一个数组克隆要快得多选择命名参数。是否有任何事实,在什么情况下/浏览器会导致性能损失?你知道的关于这个主题的任何文章?

I remember reading somewhere that accessing the arguments Object directly can be significantly slower than an Array clone or than the obvious choice of named arguments. Is there any truth to that and under what circumstances / browsers does it incur a performance penalty to do so? Any articles on the subject you know of?

更新来自 http://bonsaiden.github.com/JavaScript-Garden/#function.arguments 使我之前阅读的内容无效...希望这个问题可以从<写了这篇文章的@ href =https://stackoverflow.com/users/170224/ivo-wetzel> @Ivo Wetzel 。

update interesting find from http://bonsaiden.github.com/JavaScript-Garden/#function.arguments that invalidates what I read previously... Hoping the question gets some more answers from the likes of @Ivo Wetzel who wrote this.

在底部该部分说:


表现神话和真相

参数对象总是创建
,只有两个例外是
的情况,它被声明为一个函数内的名字
或者一个
正式名称参数。无论是否使用
都无关紧要。

The arguments object is always created with the only two exceptions being the cases where it is declared as a name inside of a function or one of its formal parameters. It does not matter whether it is used or not.

这与 http://www.jspatterns.com/arguments-considered-harmful/ ,其中说明:


但是,出于以下原因使用
参数不是一个好主意:

However, it's not a good idea to use arguments for the reasons of :


  • 表现

  • 安全

参数对象不是每次调用函数时自动创建,JavaScript引擎只会按需创建它(如果使用的话)。而且这种创造在性能方面并不是免费的。使用参数与不使用参数之间的差异可能会慢1.5到4倍,具体取决于浏览器

The arguments object is not automatically created every time the function is called, the JavaScript engine will only create it on-demand, if it's used. And that creation is not free in terms of performance. The difference between using arguments vs. not using it could be anywhere between 1.5 times to 4 times slower, depending on the browser

不能两者都是正确的,那么它是哪一个?

clearly, can't both be correct, so which one is it?

ECMA顽固Dmitrty Soshnikov说:

ECMA die-hard Dmitrty Soshnikov said:


究竟JavaScript引擎是
的意思?你在哪里得到这个确切的
信息?虽然,在一些
实现中可能是真的(是的,这是好的
优化,因为所有需要的信息都是关于
的上下文可用于解析代码的
,所以没有必要如果在解析时找不到
,则创建
参数对象,但是如你知道
ECMA-262-3语句那样,每次创建
object 参数
上输入执行上下文。

Which exactly "JavaScript engine" is meant? Where did you get this exact info? Although, it can be true in some implementations (yep, it’s the good optimization as all needed info about the context is available on parsing the code, so there’s no need to create arguments object if it was not found on parsing), but as you know ECMA-262-3 statements, that arguments object is created each time on entering the execution context.


推荐答案

这是一些q& d测试。使用预定义的参数似乎是最快的,但这样做并不总是可行的。如果函数的arity事先是未知的(因此,如果一个函数可以或必须接收可变数量的参数),我认为一次调用 Array.prototype.slice 最有效的方法,因为在这种情况下使用<$ c的性能损失 $ c> arguments object是最小的。

Here's some q&d testing. Using predefined arguments seems to be the fastest, but it's not always feasible to do this. If the arity of the function is unknown beforehand (so, if a function can or must receive a variable amount of arguments), I think calling Array.prototype.slice once would be the most efficient way, because in that case the performance loss of using the arguments object is the most minimal.

这篇关于javascript函数和参数对象,是否涉及成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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