9

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.

Community
  • 1
  • 1
user148273
  • 253
  • 3
  • 10
  • I don't know. You should try it empirically if you really care, but my guess is that there is no difference. – Brian Donovan Jan 20 '11 at 18:48
  • 3
    See: [Does use of anonymous functions affect performance?](http://stackoverflow.com/questions/80802/does-use-of-anonymous-functions-affect-performance) (short answer: it depends on the context of the expression). – Shog9 Jan 20 '11 at 18:49
  • Take a look at this jsPerf: https://jsperf.com/anonymus-vs-declaration-vs-expression I did run this on Firefox 56, Chrome 62.0.3202 and IE 11 - in all cases performance differences between different approaches were minuscule. – Maz T Dec 13 '17 at 21:29

2 Answers2

13

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

  • A function declared with a function declaration statement (second sample) has a name that will show up in stack traces etc.
  • Function declaration statements are "hoisted" to the top of their blocks and interpreted as if they actually appeared there, before any other statements in the function run.

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

Pointy
  • 371,531
  • 55
  • 528
  • 584
  • Right. The time it takes to *execute* a function declaration is tiny compared to the time it takes to compile the function in the first place (assuming the functions actually do have some code in them). – Jason Orendorff Jan 20 '11 at 18:52
  • 2
    I understand semantic differences are more important, but I do want to know if there is any performance impact. Does this mean that memory managements, allocation, etc. inside javascript will be the same regardless of how function is declared? – user148273 Jan 20 '11 at 19:27
  • Yes, except in quirky exotic circumstances that somebody might dream up, there's no difference to how those functions work once you've called them; they're full-blown real functions either way. Having a name is nice because the name shows up in stack traces. The syntax allows for a name to be supplied whether or not it's a real definition statement, but browser bugs make that an unwise thing to do (which is too bad). – Pointy Jan 20 '11 at 19:33
  • Carakan at least gives the name of the variable it is bound to in stacktraces. – gsnedders Jan 20 '11 at 19:50
  • @gsnedders I don't know what "carakan" is but that's nice, for those times when there *is* a variable. (Probably there is, pretty often I guess.) – Pointy Jan 20 '11 at 19:56
  • @Pointy: Opera 10.50 and above's JavaScript engine. – gsnedders Jan 20 '11 at 23:12
  • It's been a few years, you might want to reword some of this, particularly "*Function definition statements*", by which I think you mean *function declarations*. Not sure "evaluated" fits either, probably "processed" is better since evaluation infers (to me at least) that something is run. – RobG Nov 10 '15 at 22:43
  • @RobG thanks. Maintaining old SO answers is not something I had planned on :) – Pointy Nov 10 '15 at 22:59
  • @Pointy—maintenance goes on forever… ;-) – RobG Nov 10 '15 at 23:19
10

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

Amjad Masad
  • 3,918
  • 17
  • 20
  • Differences are *miniscule* in any case. As I commented on the question, context is what matters: http://jsperf.com/fn-expression-vs-statement/3 – Shog9 Jan 20 '11 at 18:59
  • Ya, one should choose whatever looks more appealing and readable. – Amjad Masad Jan 20 '11 at 19:02
  • 1
    Oddly enough, in Firefox 37, the behavior was the complete opposite, but the difference was enormous. The expression did 974 million ops/s, while the declaration (statement) did only 4 million. – Chris Middleton Apr 30 '15 at 13:22
  • 1
    Seeing an enormous difference in recent chrome v55+ – jfunk Apr 18 '17 at 02:49
  • [Don't trust these artificial microbenchmarks](http://mrale.ph/blog/2012/12/15/microbenchmarks-fairy-tale.html). – Bergi May 17 '17 at 22:43