gaearon / react-proxy

Proxies React components without unmounting or losing their state
460 stars 50 forks source link

Use WeakMap if available to avoid O(n) lookup #62

Open sheerun opened 8 years ago

sheerun commented 8 years ago

Supersedes #57

gaearon commented 8 years ago

Do you want to extract this to an npm module? Inspired by your previous PR I implemented something similar here because I need it in that project too. Ideally I’d prefer this be an external module I could depend on in both projects.

sheerun commented 8 years ago

Isn't https://github.com/medikoo/es6-weak-map exactly what you're looking for?

sheerun commented 8 years ago
var WeakMap;

if (useWeakMap) {
  WeakMap = require('es6-weak-map');
} else {
  WeakMap = require('es6-weak-map/polyfill');
}
gaearon commented 8 years ago

I don’t really care that much about real WeakMap semantics—it’s fine to leak memory in older browsers in development mode during hot reloading. The problem with polyfills is they sometimes have tricky edge cases or may cause weird problems in some environments. Ideally I want to:

sheerun commented 8 years ago

Do you wish to restrict input values to only objects? (just like WeakMap)

gaearon commented 8 years ago

Yeah, so that WeakMap case works.

sheerun commented 8 years ago

I think https://github.com/WebReflection/es6-collections implementation should suit you except:

  1. It overrides global WeakMap
  2. It also implements few other collections

I could fix both and extract code to the new module. OK?

gaearon commented 8 years ago

I’d really prefer my implementation in this case. The surface area I need is very small, and putting components into “slots” by their displayName/name reduces the lookup time for the common case. I think we can benefit from having a very specific use case (map by component) here.

sheerun commented 8 years ago

OK, I think I have full picture. I'll try to come up with something soon.

wkwiatek commented 7 years ago

Have you come up with something new? Or should we just close this?

sheerun commented 7 years ago

@wkwiatek Thanks for reminding.. I've created https://github.com/sheerun/component-map and update this PR to use it. ComponentMap includes lots of tests that also verify proper garbage collection. Hope you like it.

sheerun commented 7 years ago

@gaearon You might want to consider using component-map it in react-hot-loader as well