constelation / monorepo

Mono repo for constelation's Components, functions, and CONSTANTS
https://constelation.github.io/monorepo/
MIT License
12 stars 3 forks source link

Micro-perf: use `bind()` #54

Open kylpo opened 7 years ago

kylpo commented 7 years ago

remove arrow function instance methods. Replace with bind() in constructor.

See https://twitter.com/_developit/status/839189848514826241 and https://twitter.com/_developit/status/839454500490526726 for why.

kylpo commented 7 years ago

More on this: Dan Abramov on Twitter: "If you don’t believe me listen to @floydophone 😊 https://t.co/XqAmpVZH2z"

bendman commented 7 years ago

For the record, I ran a quick test to check memory usage, rendering 100k components that have a method bound using:

  1. Constructor bind - this.method = this.method.bind(this) in the constructor
  2. Arrow bind - this.method = () => {}
  3. Decko bind - @bind from the decko package above the method

Method 1 took 240MB Method 2 took 219MB Method 3 took 211MB

I'd be interested in how arrow uses less memory than the constructor bind, because arrow binding is compiled to redefine the method in a closure during every instantiation, whereas constructor binding just calls bind on a pre-defined prototype method (although there is some overhead and a possible closure created when defining the prototype method). Either way, the decko bind seems to be the least memory intensive. An interested test if anyone cares would be to destroy the components and rebuild them, and see how efficient garbage collection is.