anthonyshort / deku

Render interfaces using pure functions and virtual DOM
https://github.com/anthonyshort/deku/tree/master/docs
3.41k stars 130 forks source link

diff.diffNode() should return empty array when no changes #412

Open TimBarham opened 8 years ago

TimBarham commented 8 years ago

Hi - I'm using diff.diffNode() and dom.update() directly.

I've noticed if I pass two vNodes with identical structure to diff.diffNode(), I still get back a complicated tree of changes full of updateChildren and updateChild changes (with the final result being a no-op).

I would like to easily detect when diff.diffNode() reports there are no changes. While I can walk the tree and figure it out, it seems more efficient if diff.diffNode() (and `diff.diffChildren()') simply returned an empty array in this scenario.

It seems this can fairly easily be done by something like the following change when returning from diffChildren():

return changes.length ? updateChildren(changes) : [];

And in the isNative case when returning from diffNode():

var changes = diffAttributes(prev, next);
var childDiff = diffChildren(prev, next, path);
if (childDiff.length) {
  changes.push(childDiff);
}

Similar changes would probably be needed for thunks (but I'm not currently using them so I haven't looked at that).

So I'm wondering - is there some philosophical reason not to just return an empty array in these scenarios?