Javascript:var functionName = function(){} vs function functionName(){}的性能 [英] Javascript: performance of var functionName = function() {} vs function functionName() {}

查看:81
本文介绍了Javascript:var functionName = function(){} vs function functionName(){}的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

使用匿名函数会影响性能吗?

在Javascript中使用函数表达式vs声明时,解析和运行时的性能影响(如果有)是什么?

What are performance implications (if any) on parse and run-time when using function expression vs declaration in Javascript?

例如,以下两种方式的性能特征和差异是什么:

For example, what are performance characteristics and differences of the following two ways:

var functionOne = function() {
    // Some code
}

或:

function functionTwo() {
    // Some code
}

注意:问题不在于声明函数是否更快,而是函数执行。

NOTE: The question is not about whether it is faster to declare function but about function execution.

推荐答案

比性能差异更重要的是这两者之间的语义差异。

Much more important than performance differences are the semantic differences between those two.


  • 使用函数声明语句声明的函数(第二个示例)的名称将显示在堆栈跟踪等中。

  • 在函数运行中的任何其他语句之前,函数声明语句被提升到它们块的顶部并被解释为它们实际出现在那里。

至少在现代运行时环境中,性能差异可能非常小,甚至可以检测到。

The performance differences are probably pretty tiny, if even detectable, at least in modern runtime environments.

这篇关于Javascript:var functionName = function(){} vs function functionName(){}的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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