AmpersandJS / ampersand-state

Core state management object.
MIT License
141 stars 75 forks source link

Derived properties are calculated unnecessarily often #238

Open janpaul123 opened 8 years ago

janpaul123 commented 8 years ago

Say you have derived properties like this:

derived: {
  A: { deps: ['X'], fn: … },
  B: { deps: ['X'], fn: … },
  C: { deps: ['X'], fn: … },
  Z: { deps: ['A', 'B', 'C'], fn: … },
}

Currently, if you change X, the function for Z will trigger 3 times, like this (every letter is a function executing): A Z B Z C Z, while it's clearly sufficient to do A B C Z (or, perhaps, to not do anything until later, as suggested in #233).

There are a few workarounds for this (denormalising derived properties, using silent: true + custom events, …?) and I might post them here later, but I haven't quite figured out the pros/cons of them.

cdaringe commented 8 years ago

interesting. makes sense. it's as though though a derived recompute needs memoization with cache size === 1