News for hackers. Stuff that matters.
Re:Javascript Fundamentals: ‘this’ keyword by sdegutis on Saturday August 25, 2018 @01:16PM (#17841618)
This is the solution I've been using, but it's not ideal. The lambda stored in updateState is recreated every time the class is instantiated, which can create problems for the GC depending on how often React creates temporary instances of Foo for its internal purposes.
Re:Javascript Fundamentals: ‘this’ keyword by ggregoire on Saturday August 25, 2018 @01:27PM (#17841654)
That's how classes work, the lambda doesn't change anything here. The GC deals with it without problems. If an instance of Foo is not used anymore, the GC will destroy it and anything bound to it.
Re:Javascript Fundamentals: ‘this’ keyword by sdegutis on Saturday August 25, 2018 @02:26PM (#17841871)
ES6 methods are defined outside the constructor so are O(1), but all class-fields are assigned inside the constructor thus O(n), so it can make a difference.
Re:Javascript Fundamentals: ‘this’ keyword by acemarke on Saturday August 25, 2018 @04:40PM (#17842476)
That statement seems wrong, or maybe I'm having trouble interpreting the way you're phrasing things (particularly with your use of "O(1)" and "O(n)").

Standard ES6 class methods are defined on the _prototype_, so only one function reference exists per method.

Binding class methods per instance, either by hand or using the Stage 3 Class Properties syntax, would result in a separate function reference per method per class instance (ie, 4 bound methods on a component class, times 5 instances of that component, would be 20 method references in memory instead of 4).

So yes, binding methods technically has some perf overhead, but most apps are unlikely to be creating thousands of instances of any given component type. To me, this is not a realistic concern. (In fact, if you think about it, you're generating _lots_ of functions every time you re-render if you're calling something like `someArray.map(() => {})` - way more than you ever would just from instantiating some components.)