Do Anonymous Functions Take Memory If Run In a loop

Viewed 127

If I have 100 Objects in an array and for each object If I define an anonymous function for each object then will it take memory for 100 function(For onClick function), if yes then how to avoid it and if No then How JS manage it?
Code Example -

var a = [{a:1},{a:2},{a:3},...{a:100}];
var html = a.map((item, i) => (
      <div className={Styles.someCss} key={i}>
        <Component
         onClick={() => { this.someFunction(item) }}
        />
      </div>
    ));
1 Answers

Yes, it will create 100 anonymous functions but that's not something one has to avoid. There is no other way of calling this.someFunction with an argument without creating a new function. You could call bind, as written in the documentation, but this creates a new object as well. So your code is totally fine.
(One hacky way around this would be to bind the item object stringified onto the DOM-Element via a data-tag, and then receiving it through event.target, but that's usually not the way-to-go.)

Related