fbjs
fbjs copied to clipboard
`shallowEqual` on Date objects gives false positives
I can't say if this is the intended behavior, but we can't use shallowEqual to compare Date objects:
d1 = new Date(Date.UTC(2010, 10, 10)) // 2010-11-10T00:00:00.000Z
d2 = new Date(Date.UTC(2010, 10, 11)) // 2010-11-11T00:00:00.000Z
shallowEqual(d1, d2) // true
This happens because Object.keys(new Date) returns an empty list, which skips the checks of each object property here: https://github.com/facebook/fbjs/blob/master/packages/fbjs/src/core/shallowEqual.js#L59
So, should we also return false when the object returns no keys?
:+1:
@reu
AFAIK it's good and expected behaviour to return true. Date is quite a special object in JS and has no normal fields since it's supported natively (take a look e.g. for JSC https://github.com/WebKit/webkit/blob/89c28d471fae35f1788a0f857067896a10af8974/Source/JavaScriptCore/runtime/DatePrototype.cpp).
So it's absolutely fine to tell that these objects are shallowly equal. I think I might be very bad idea to return false, because then in a case of the same dates it will return false as well.