ractivejs/ractive — pull request #1312 by Rich-Harris (2014-10-05 21:20:11+00:00)
Title: Lazy computed properties

Thought I'd have a stab at this (see https://github.com/ractivejs/ractive/issues/1293#issuecomment-57936956). It seems like a fairly good approach - the code is a bit simpler, and it does save a bunch of recalculations if you have computed values that depend on other computed values, etc.

It *does* mean that computations will occasionally happen unnecessarily:

```js
ractive = new Ractive({
el: fixture,
template: '#template',
data: {
number: 42
},
computed: {
numberIsPositive: function () {
console.log( 'computing numberIsPositive' );
return this.get( 'number' ) > 0;
},
description: function () {
console.log( 'computing description' );
return 'the number is ' + ( this.get( 'numberIsPositive' ) ? 'positive' : 'negative' );
}
}
});
```

Whereas previously, setting `number` to another positive number would have only caused `numberIsPositive` to recompute (because its value doesn't change, it wouldn't have caused any further changes), it now causes both `numberIsPositive` and `description` to recompute (`numberIsPositive` is identified as a dependant of `number`, and `description` is identified as a dependant of `numberIsPositive`). In real-world scenarios, I'd expect this trade-off to be very much worth it.

Right now, this breaks a load of tests, because of differences between the implementation of `Computation` and `Evaluator`. Working on that now.