7

Consider:

function f1() {
  function n11() { .. lots of code .. };
  const n12 = () => { .. lots of code .. };
  return n11()+n12()+5;
}

const f2 = () => {
  function n21() { .. lots of code .. };
  const n22 = () => { .. lots of code .. };
  return n21()+n22()+5;
}

I am trying to understand the memory implications of calling f1 and f2.

Regarding n11, this answer says:

For some very small and normally inconsequential value of "wasted". JavaScript engines are very efficient these days and can perform a wide variety of tricks/optimizations. For instance, only the function-object (but not the actual function code!) needs to be "duplicated" internally. There is no "wasting" problem without an actual test-case that shows otherwise. This idiom (of nested and anonymous functions) is very common in JavaScript and very well-optimized for.

However I want to know if this also apply to arrow functions (ie n12, n21 and n22) .. will the overhead only be a function-object as above or will the entire nested function code be duplicated every time f1/f2 are called ?

thx!

Community
  • 1
  • 1
kofifus
  • 17,260
  • 17
  • 99
  • 173
  • 1
    Probably depends on the JavaScript engine. Which one are you curious about? – Felix Kling Sep 22 '17 at 23:12
  • more of a general question, but targeting modern browsers .. are some known to be better/worse at this ? – kofifus Sep 22 '17 at 23:13
  • 1
    Well, "in general" engines don't have to do any of these things. The spec doesn't require them to do that. However, I don't see why there should be any difference between arrow functions and "normal" functions in that regard. But if you want to know specifically whether an engine is doing that or not, you'd have to look at its source code I guess. – Felix Kling Sep 22 '17 at 23:14
  • https://stackoverflow.com/questions/44030645/are-arrow-functions-faster-more-performant-lighter-than-ordinary-standalone-f this answer claims that arrow functions are just sugar for regular functions. So I would imagine they have the same effect on memory as regular functions. – djfdev Sep 22 '17 at 23:25
  • why nest the functions? – Ronnie Royston Sep 22 '17 at 23:30
  • 1
    Ron - so they can access the surrounding closure – kofifus Sep 23 '17 at 01:16

1 Answers1

6

There's absolutely no reason why an implementaton would need to do anything different for arrow functions than traditional functions with regard to the sharing of code between different closures of the same function. The only difference between arrow functions and traditional functions is that arrow functions save the this value. This can be done using the same mechanism as is already provided for the Function.prototype.bind() method. An arrow function is mostly just syntactic sugar.

func = () => { body };

is roughly equivalent to:

func = function() { body }.bind(this);

(This is a slight simplification, since arrow functions also don't get an arguments object, but that shouldn't affect what you're asking.)

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thx! do you have any information that nested functions (arrow or regular) are in fact optimized ? – kofifus Sep 22 '17 at 23:23
  • 1
    There's nothing about nested functions that would require them to duplicate code. Everything involving variable scopes is handled in the function's closure data, not in the code. – Barmar Sep 22 '17 at 23:24