10

Does any one know about optimization effects of passing in a variable via function arguments versus having the variable available via a closure? It seems like passing in variables via function arguments would be faster since objects are copied by reference (so fast copy time) and climbing the scope environments of the function requires checking the environment at each level. Here's the gist of what I mean

a = 5;
b = function() {
  alert(a);
}
b();

versus

a = 5;
b = function(c) {
  alert(c);
}
b(a);

Which in theory performs faster?

elju
  • 445
  • 1
  • 5
  • 15
  • 1
    Faster than what? By what criteria? ECMA-262 does not define implementation, so in theory neither is faster. Identifiers must be resolved on a scope chain regardless of which execution context they belong to, so *in theory* local variables should be resolved faster. But counter intuitively, it is known that globals are faster in some environments. So "testing will tell" and "expect different results in different browsers" applies. – RobG Sep 09 '13 at 06:17
  • 1
    But they're not interchangeable...First function depends on `a` while the second one doesn't, so you'd use them in different situations. – elclanrs Sep 09 '13 at 06:18
  • @elclanrs—I understood the question as whether it's faster to resolve local variables or those father up the scope chain (which are established through closures). – RobG Sep 09 '13 at 06:20
  • The http://jsperf.com/scope-vs-passed-test123123 tests seem to show that speed varies upon browsers. I'd be more interested however as to the memory use difference, and ultimate browser performance when it comes to the js garbage collector. I have a slightly related question to that effect: http://stackoverflow.com/q/20673056/1647538 – hexalys Dec 19 '13 at 06:52
  • Could you please explain what the benefit of using the closure is as opposed to the function argument? – Yamcha Sep 27 '15 at 22:36

3 Answers3

5

I had the same question a little while ago, so I slapped together a quick'n'dirty benchmark. It appears that most popular browsers (surprisingly) prefer looking up in the scope (FF24 very much so).

I hope this answers your question.

antichris
  • 2,827
  • 1
  • 22
  • 18
  • The difference seems to be not much, but this kind of reference is still good (at least to show it does not make a lot of difference). For me, with Opera 12.16 on i686, it's a little bit faster with “passed”. – Hibou57 Sep 20 '14 at 22:07
  • jsPerf is down for some time. Any chance, you could provide the code? – LeZuse Sep 07 '16 at 10:57
3

climbing the scope environments of the function requires checking the environment at each level

Only theoretically. In fact, since the scope chain is not dynamic, this can and will be optimized to a static reference.

passing in variables via function arguments would be faster since objects are copied by reference (so fast copy time)

Even it is very fast, they still need to be copied. The function needs to allocate extra memory for them, while the reference to the closure does not anything like that.


If you can put a value in the closure scope, do it. It's only practical, you want to build a closure. If you don't want and need variable arguments in your function, use parameters. Use the more readable alternative.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
-3

It all depends. Don't worry about it unless it's a big issue in the future.

Joe Simmons
  • 1,828
  • 2
  • 12
  • 9