developit / linkstate

Bind events to state. Works with Preact and React.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

So what's about memoization?

throwaway1496385509 opened this issue · comments

Docs say it's memoized and I was curious about how bad memory usage can get over time because of it and just wanted to see what approach did you choose with it. Checked the source code and haven't found any trace of memoization anywhere.

Quick test shows that linkstate(state, 'foo') !== linkstate(state, 'foo'). Maybe I'm missing something.

What did you mean by

🤔 Why?

linkState() is memoized: it only creates a handler once for each (key, eventPath) combination.

This is important for performance, because it prevents handler thrashing and avoids allocations during render.

🤔

From preact guide:

Multiple calls to linkState(component, name) with the same component and name are cached, so there is essentially no performance penalty.

yeah i dont see memoization anywhere

@developit I’d like to see your answer too ;-)

Ack, you're right! I had forgotten to port over the memoization when I pulled this out of Preact, since it was being done in Component.prototype.linkState, not the underlying method (that got ported here).

Here's what needs to be added:

function linkState(component, key, eventPath) {
  let c = component._linkedStates || (component._linkedStates = {});
  return c[key+'\n'+eventPath] || (c[key+'\n'+eventPath] = function(e) {
    // existing inner function
  });
}

Fixed by #7! :D