3

With ES6 javascript offers the use of arrow functions like:

var quadr= x => x * x;

I know this influences the binding of this, arguments, super and new.target. source

This is certainly usefull to ensure a correct this in every scope. But i'm questioning if the binding affects the memory footprint of the script. Are less objects in the execution context? Does this influence the performance of the functions?
And what happens (qua memory and allocation) when we reference a this inside an arrow function, like:

function person(){
  this.age=0;
  //it's my birthday so...
  ()=>{
     this.age++;
  }
}
Bee157
  • 576
  • 6
  • 14
  • do not care. its good enough.. – Jonas Wilms Jul 30 '17 at 16:33
  • 1
    almost all that you ask will depend on browser implementation. the way i see it there will be a small increase in memory use because of the binding probably similar to the use of anonymouns.bind(this) or something like that but it should not prevent you from using it because it will safe you a lot of headaches when you need a constant execution context. – Dayan Moreno Leon Jul 30 '17 at 16:38
  • It's a closure and will perform just like any other closures. – Bergi Jul 30 '17 at 16:59

1 Answers1

1

And what happens (qua memory and allocation) when we reference a this inside an arrow function?

Whenever a function is called, a lexical environment is created, which has a reference to the lexical environment in which it was defined. So imagine this:

function a(){
  function b(){
  }
  b.call(bContext)
}
a()

the lexical environments have a value pointing to the current context (aka this), and a reference of their enclosing lexical environment. so if the inner function gets called the environment would look like this (pseudocode):

a{
  this:window
}
b{
 parent:a
 this:bContext
 }

Lets imagine b would be an arrow function:

function a(){
  b=()=>{};
  b();
}

then there would simply be no context in it, and it would be looked up in a's environment:

a{
  this:window//
}
b{
 parent:a
 }

Therefore, arrow functions are actually a bit more leightweight then the regular ones.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151