FunctionExpression和内存消耗 [英] FunctionExpression's and memory consumptions

查看:47
本文介绍了FunctionExpression和内存消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种常见的分叉模式。返回函数如下面的示例:

函数bind(fn,context){

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

if(args.length){

return function(){

fn.apply (context,args);

}

}

返回函数(){

return fn.call(上下文);

}

}


运行时速度的好处是显而易见的,但我被告知有

在这种情况下增加了内存消耗。在调用`bind`时是否创建了2个函数

对象?我假设'不是

的情况,因为那些不是FunctionDeclaration',而是

FunctionExpression'(因此它们不应该被评估为最重要的)

输入执行上下文时)。在永不评估的块中,FunctionExpression是否包含

创建Function对象?

如果FunctionExpression包含在

`return`条款中会有所不同吗?


我找不到相关部分在规范中,并将不胜感激

对此事的任何见解。


-

kangax

There''s a common pattern of "forking" a returning function as in the
following example:

function bind(fn, context) {
var args = Array.prototype.slice.call(arguments, 2);
if (args.length) {
return function() {
fn.apply(context, args);
}
}
return function() {
return fn.call(context);
}
}

The runtime speed benefits are obvious, but I''ve been told that there
is an increased memory consumption in such cases. Are there 2 Function
objects created when `bind` is being called? I assume that''s not the
case, since those are not FunctionDeclaration''s, but rather
FunctionExpression''s (and so they should not be evaluated foremost
when execution context is entered). Are FunctionExpression''s contained
within blocks that are never evaluated create Function objects? Does
it make a difference if FunctionExpression is contained within a
`return` clause?

I can''t find relevant parts in the specification and would appreciate
any insights on this matter.

--
kangax

推荐答案

kangax写道:
kangax wrote:

有一种常见的分叉模式返回函数如下面的示例:

函数bind(fn,context){

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

if(args.length){

return function(){

fn.apply (context,args);

}

}

返回函数(){

return fn.call(上下文);

}

}


运行时速度的好处是显而易见的,但我被告知有

在这种情况下增加了内存消耗。在调用`bind`时是否创建了2个函数

对象?我假设'不是

的情况,因为那些不是FunctionDeclaration',而是

FunctionExpression'(因此它们不应该被评估为最重要的)

输入执行上下文时)。在永不评估的块中,FunctionExpression是否包含

创建Function对象?

如果FunctionExpression包含在一个

`return`子句中会有所不同吗?
There''s a common pattern of "forking" a returning function as in the
following example:

function bind(fn, context) {
var args = Array.prototype.slice.call(arguments, 2);
if (args.length) {
return function() {
fn.apply(context, args);
}
}
return function() {
return fn.call(context);
}
}

The runtime speed benefits are obvious, but I''ve been told that there
is an increased memory consumption in such cases. Are there 2 Function
objects created when `bind` is being called? I assume that''s not the
case, since those are not FunctionDeclaration''s, but rather
FunctionExpression''s (and so they should not be evaluated foremost
when execution context is entered). Are FunctionExpression''s contained
within blocks that are never evaluated create Function objects? Does
it make a difference if FunctionExpression is contained within a
`return` clause?



未达到的表达式应该没有效果。我们可以看到一个调用表达式: -


函数unreachableExpression(){

if(true)return;

alert(") ;恐慌!!!我没有功能测试警报!");

}


返回语句也没有效果: -


函数unreachableStatement(){

如果(true)返回true;

返回false;

每次调用绑定函数时,都会创建一个新函数,而不是
两个。如果arguments.length为0,则第二个函数表达式将返回

,但总是会出错,因为fn将是未定义的。你可能要检查if(arguments.length 2)。


函数f(){

alert(this.type);

}


var e = bind(f,{sound:" elephant"});


第二个函数不需要args属性,因此创建一个

数组是不必要的。


函数bind(fn,上下文){

var args;

if(arguments.length 2){

args = Array.prototype.slice.call(arguments, 2);

返回函数(){

fn.apply(context,args);

}

}

返回函数(){

返回fn.call(context);

}

}


Garrett

-

comp.lang.javascript常见问题< URL: http://jibbering.com/faq/ >

Unreached expressions should have no effect. We can see a call expression:-

function unreachableExpression() {
if(true) return;
alert("Panic!!! I did not feature-test alert!");
}

And a return statement would also have no effect:-

function unreachableStatement() {
if(true) return true;
return false;
}

Each time that bind function is called, a new function is created, not
two. If arguments.length were 0, the second function expression would be
returned, but would always err because fn would be undefined. You
probably meant to check "if(arguments.length 2)".

function f(){
alert(this.type);
}

var e = bind(f, { sound: "elephant" });

The second function does not need the args property, so creating an
array would be unnecessary.

function bind(fn, context) {
var args;
if (arguments.length 2) {
args = Array.prototype.slice.call(arguments, 2);
return function() {
fn.apply(context, args);
}
}
return function() {
return fn.call(context);
}
}

Garrett
--
comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >


10月26日,9:42 * pm,kangax< kan ... @ gmail.comwrote:
On Oct 26, 9:42*pm, kangax <kan...@gmail.comwrote:

有一种常见的分叉模式返回函数如下面的




函数绑定(fn,context){

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

* if(args.length){

* * return function(){

* * * fn.apply(context,args);

* *}

*}

* return function(){

* *返回fn.call(上下文);

*}


}


运行时速度的好处是显而易见的,但我被告知,在这种情况下,
会增加内存消耗。在调用`bind`时是否创建了2个函数

对象?我假设'不是

的情况,因为那些不是FunctionDeclaration',而是

FunctionExpression'(因此它们不应该被评估为最重要的)

输入执行上下文时)。在永不评估的块中,FunctionExpression是否包含

创建Function对象?是
There''s a common pattern of "forking" a returning function as in the
following example:

function bind(fn, context) {
* var args = Array.prototype.slice.call(arguments, 2);
* if (args.length) {
* * return function() {
* * * fn.apply(context, args);
* * }
* }
* return function() {
* * return fn.call(context);
* }

}

The runtime speed benefits are obvious, but I''ve been told that there
is an increased memory consumption in such cases. Are there 2 Function
objects created when `bind` is being called? I assume that''s not the
case, since those are not FunctionDeclaration''s, but rather
FunctionExpression''s (and so they should not be evaluated foremost
when execution context is entered). Are FunctionExpression''s contained
within blocks that are never evaluated create Function objects? Does



No.

No.


如果FunctionExpression包含在
$中它会有所不同b $ b`return`条款?
it make a difference if FunctionExpression is contained within a
`return` clause?



什么是退货条款?

What is a return clause?


10月26日晚上10点49分,dhtml< dhtmlkitc ... @ gmail.comwrote:
On Oct 26, 10:49 pm, dhtml <dhtmlkitc...@gmail.comwrote:

kangax写道:
kangax wrote:

有一个共同的模式"分叉"返回函数如下面的示例:
There''s a common pattern of "forking" a returning function as in the
following example:


function bind(fn,context){

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

if(args.length){

return function(){

fn.apply(context,args);

}

}

返回函数(){

返回fn.call(context);

}

}
function bind(fn, context) {
var args = Array.prototype.slice.call(arguments, 2);
if (args.length) {
return function() {
fn.apply(context, args);
}
}
return function() {
return fn.call(context);
}
}


运行时速度的好处是显而易见的,但我被告知,在这种情况下,
会增加内存消耗。在调用`bind`时是否创建了2个函数

对象?我假设'不是

的情况,因为那些不是FunctionDeclaration',而是

FunctionExpression'(因此它们不应该被评估为最重要的)

输入执行上下文时)。在永不评估的块中,FunctionExpression是否包含

创建Function对象?

如果FunctionExpression包含在

`return`子句中会有所不同吗?
The runtime speed benefits are obvious, but I''ve been told that there
is an increased memory consumption in such cases. Are there 2 Function
objects created when `bind` is being called? I assume that''s not the
case, since those are not FunctionDeclaration''s, but rather
FunctionExpression''s (and so they should not be evaluated foremost
when execution context is entered). Are FunctionExpression''s contained
within blocks that are never evaluated create Function objects? Does
it make a difference if FunctionExpression is contained within a
`return` clause?



未达到的表达式应该没有效果。我们可以看到一个调用表达式: -


函数unreachableExpression(){

if(true)return;

alert(") ;恐慌!!!我没有功能测试警报!;;


}


并且返回声明也没有效果: -


函数unreachableStatement(){

如果(true)返回true;

返回false;


}


每次调用绑定函数时,都会创建一个新函数,而不是
。如果arguments.length为0,则第二个函数表达式将返回

,但总是会出错,因为fn将是未定义的。你可能要检查if(arguments.length 2)。


函数f(){

alert(this.type);


}


var e = bind(f,{sound:" elephant"});


第二个函数不需要args属性,因此创建一个

数组是不必要的。


函数bind(fn,context){

var args;

if(arguments.length 2){

args = Array.prototype.slice。 call(arguments,2);

return function(){

fn.apply(context,args);

}

}

返回功能(){

返回fn.call(上下文);

}


}


Unreached expressions should have no effect. We can see a call expression:-

function unreachableExpression() {
if(true) return;
alert("Panic!!! I did not feature-test alert!");

}

And a return statement would also have no effect:-

function unreachableStatement() {
if(true) return true;
return false;

}

Each time that bind function is called, a new function is created, not
two. If arguments.length were 0, the second function expression would be
returned, but would always err because fn would be undefined. You
probably meant to check "if(arguments.length 2)".

function f(){
alert(this.type);

}

var e = bind(f, { sound: "elephant" });

The second function does not need the args property, so creating an
array would be unnecessary.

function bind(fn, context) {
var args;
if (arguments.length 2) {
args = Array.prototype.slice.call(arguments, 2);
return function() {
fn.apply(context, args);
}
}
return function() {
return fn.call(context);
}

}



谢谢,加勒特。

这很有道理。


因此,据我所知,函数表达式在未实现中。块

与未达到中的​​任何其他表达式没有什么不同。阻止(在
意义上它不应该执行,因此不应该分配任何内存)。规范实际上是否定义了这样的行为(对象初始化)或者是实现了吗?

Thanks, Garrett.
That makes much sense.

So, as far as I understand, function expression in "unreached" block
is no different than any other expression in "unreached" block (in a
sense that it shouldn''t be executed and so shouldn''t allocate any
memory). Does spec actually define such behavior (object
initialization) or is it left up to an implementation?


>

Garrett


-

comp.lang.javascript FAQ< URL:http://jibbering.com/faq/>
>
Garrett

--
comp.lang.javascript FAQ <URL:http://jibbering.com/faq/>



-

kangax

--
kangax


这篇关于FunctionExpression和内存消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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