van
van copied to clipboard
Reference comparison
trafficstars
The code uses !== to compare old and new values.
The problem is, if at least one of the values is NaN the condition will always be true
NaN !== 0; // is true
NaN !== NaN; // is also true!
We can fix the problem by using Object.is:
Object.is('0', 0); // false
Object.is(0, -0); // false
Object.is(NaN, 0); // false
Object.is(NaN, NaN); // true
Thanks @Bassel-Bakr for the feedback!
Here are my 2 cents on the issue:
- Neither
!==or!Object.isis perfect solution. For!==, we haveNaN !== NaNbeingtrue. For!Object.is, we have!Object.is(0, -0)beingtrue. - This is probably not a significant issue. The worst outcome is we would fail to skip propagating state changes and re-render DOM when we indeed can skip (i.e.: when the new value in the
s.val = <new value>call is semantically the same as the existing one). Thus we're just wasting some computation in rare circumstances. This will not cause any semantic issue for apps built with VanJS. - Among 2 options, I personally believe
!==is slightly better than!Object.is. We might encounter the comparison between0and-0more often than the comparison betweenNaNandNaN, asNaNshouldn't usually occur for a semantically correct app.