简体   繁体   English

匿名函数 VS 常量函数 - javascript

[英]Anonymous function VS const function - javascript

Following from this discussion:从这个讨论开始:

I have a doubt about function declaration in JavaScript.我对 JavaScript 中的函数声明有疑问。

By anonymous function declaration I mean something like this ( https://en.wikibooks.org/wiki/JavaScript/Anonymous_Functions ):通过匿名函数声明,我的意思是这样的( https://en.wikibooks.org/wiki/JavaScript/Anonymous_Functions ):

var myFunction = function (fruit){
    alert('I like ' + fruit);
}

and by const I mean this: const 我的意思是:

const myfunction = (fruit) => alert('I like ' fruit);

Is it faster to use an anonymous function, or use a const?使用匿名函数还是使用 const 更快? I have read that using const allows for compile optimizations in JavaScript.我读过使用 const 可以在 JavaScript 中进行编译优化。 Is there any reason why I should use one instead of the other?有什么理由我应该使用一个而不是另一个?

Is this even relevant?这甚至相关吗?

Anonymous function VS const function匿名函数 VS 常量函数

Presumably you're comparing估计你在比较

var func = function() { };
// or
let func = function() { };

with

const func = function() { };

The primary reason for doing that isn't optimization.这样做的主要原因不是优化。 It's documenting via code that you never intend to change func , and having the engine protect you from accidentally doing so.它通过代码记录您从不打算更改func ,并让引擎保护您免于意外这样做。 (I should also note that as of ES2015, none of those functions is anonymous. They all have the name func , because ES2015 adds rules for assigning names to functions created via "anonymous" function expressions based on context, including a rule for simple assignments like the above.) (我还应该注意,从 ES2015 开始,这些函数都不是匿名的。它们都具有名称func ,因为 ES2015 添加了为通过基于上下文的“匿名”函数表达式创建的函数分配名称的规则,包括简单分配的规则和上面一样。)

But regarding optimization:但关于优化:

As with most JavaScript optimization questions, the answer is: It depends .与大多数 JavaScript 优化问题一样,答案是:这取决于. In theory, using const if you never mean to change the value in func means that the JavaScript engine has the option of optimizing on the assumption that the value will never change.从理论上讲,如果您从不打算更改func中的值,则使用const意味着 JavaScript 引擎可以选择在该值永远不会更改的情况下进行优化。 Note that it doesn't exempt the engine from handling the possibility the symbol will be shadowed in a nested scope or similar.请注意,它并不能免除引擎处理符号将在嵌套范围或类似范围内隐藏的可能性。

Whether the engine actually does optimize in a meaningful way based on that knowledge the value wont' change will depend on the engine's implementation, and likely will vary from engine to engine.引擎是否确实基于该知识以有意义的方式进行优化,值不会改变将取决于引擎的实现,并且可能会因引擎而异。

Whether that makes looking up the function via the constant in order to call it any faster than looking it up via the variable will depend on the engine's implementation, as well.这是否使得通过常量查找函数以便调用它比通过变量查找更快也取决于引擎的实现。

Whether any absolute difference translates into actual gains in your program will depend on the above, how your program is structured, how frequently you're using the function, how the lookup time compares with what the function is actually doing (eg, swamping), etc.任何绝对差异是否转化为程序中的实际收益将取决于上述内容、程序的结构、使用函数的频率、查找时间与函数实际执行的时间的比较(例如,swamping),等等

It depends.这取决于。 :-) :-)

If you run into a situation where you think the lookup time is causing a real-world problem, profile it and see if it makes a difference.如果您遇到您认为查找时间导致实际问题的情况,请对其进行分析并查看它是否有所作为。


Re your edit:重新编辑:

By anonymous function declaration I mean something like this:通过匿名函数声明,我的意思是这样的:

 function myFunction(fruit){ alert('I like ' + fruit); };

and by const I mean this: const 我的意思是:

 const myfunction = (fruit) => alert('I like ' fruit);

That first one isn't an anonymous function.第一个不是匿名函数。 (Actually, neither of them is .) It's a function named myFunction , created via a function declaration . (实际上,它们都不是。)这是一个名为myFunction的函数,通过函数声明创建。 (And being a declaration, there's no need for the ; at the end.) Function declarations cannot create anonymous functions,¹ the name is a required part of the declaration.² (作为一个声明,最后不需要; 。)函数声明不能​​创建匿名函数,¹名称是声明的必需部分。²

That said, it doesn't really matter, as once the function is created (which happens at a different time from the expressions I showed above), it behaves very much like the var func = ... example in terms of how func is resolved, whether you can change func , etc.也就是说,这并不重要,因为一旦创建了函数(它发生在与我上面显示的表达式不同的时间),它的行为就非常像var func = ...例子func是已解决,是否可以更改func

Your second example varies from your first in not one, but three important ways:您的第二个示例与您的第一个示例在三个重要方面不同:

  1. It assigns the function reference to a constant.它将函数引用分配给一个常量。

  2. It uses an arrow function, rather than a function function (which for lack of a better term I'm going to call a "simple" function).它使用箭头函数,而不是function函数(由于没有更好的术语,我将称之为“简单”函数)。

  3. Your arrow function version returns the result of calling alert (because you're using a concise body on the arrow function).您的箭头函数版本返回调用alert的结果(因为您在箭头函数上使用了简洁的主体)。 Your declaration version doesn't.您的声明版本没有。

We've already dealt with any performance aspect of #1.我们已经处理了#1 的任何性能方面。 #3 is, I suspect, unlikely to matter.我怀疑,#3 不太重要。

Re #2 (it being an arrow function): Calling an arrow function requires less work than calling a simple function (in theory). Re #2(它是一个箭头函数):调用一个箭头函数比调用一个简单的函数需要更少的工作(理论上)。 The engine doesn't have to set up the arguments pseudo-object, doesn't have to create a this binding.引擎不必设置arguments伪对象,也不必创建this绑定。 But if the arrow function uses this , it requires more work to look it up (it's like a variable in the outer scope the function closes over).但是如果箭头函数使用this ,则需要做更多的工作来查找它(它就像函数关闭的外部范围中的变量)。 But again, that's the theory;但是,这就是理论。 engines can optimize as long as side-effects are not apparent.只要副作用不明显,引擎就可以优化。 For instance, if you don't use arguments in your code, modern engines avoid creating it anyway, even for simple functions.例如,如果您不在代码中使用arguments ,现代引擎无论如何都会避免创建它,即使对于简单的函数也是如此。 And I'd expect optimization around arrow function usage of this to be pretty good, since the this they see cannot change once the function exists.而且我希望围绕this的箭头函数使用的优化非常好,因为一旦函数存在,他们看到的this就不能改变。

So (wait for it) : It depends.所以(等待它) :这取决于。


¹ "Function declarations cannot create anonymous functions" - There's one thing that seems like an exception that rule, which is that export default function() { /*...*/ }; ¹ “函数声明不能​​创建匿名函数” - 有一件事似乎是该规则的一个例外,那就是export default function() { /*...*/ }; is a function declaration (yes, really) with no explicit name;是一个没有明确名称的函数声明(是的,真的); the name default is used, though, so it doesn't create an anonymous function.但是,使用名称default ,因此它不会创建匿名函数。

² "...the name is a required part of the declaration..." Except in the export default example above. ² “...名称是声明的必需部分...”上面的export default示例除外。 That's the one exception.这是一个例外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM