Possible Duplicate:
Does use of anonymous functions affect performance?
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
}
or:
function functionTwo() {
// Some code
}
NOTE: The question is not about whether it is faster to declare function but about function execution.
The var statement declares a variable. Variables are containers for storing information. Creating a variable in JavaScript is called "declaring" a variable: var carName; After the declaration, the variable is empty (it has no value).
Programmers can create a single variable using the var, let, or const keywords and assign the function expression to that. However, creating a variable with the const keyword is recommended to assign the function as the function expression always remains constant.
JavaScript functions are defined with the function keyword. You can use a function declaration or a function expression.
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.
Here is a JSPerf Link, try testing on multiple browser, because results tend to vary. In chrome 10 the function statement got better score. http://jsperf.com/fn-expression-vs-statement
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With