Is it safe to compare Date objects?
In JavaScript, comparing Date objects provides numerous opportunities for bugs:
Does ClojureScript have the same pitfalls? In cljs, Date equality checks are by value. However, other checks (<
/>
) fall through to JavaScript. Simple checks are fine, but if you’re doing much work with dates, you’re likely better off using a library like cljs-time.
The Details
The following isn’t necessary to know, but it does provide some interesting details, depending on your definition of “interesting.”
So how does ClojureScript handle Date comparison?
The ClojureScript equality check we care about is:
Date objects implement the IEquiv
protocol, which is where we can find the implementation of -equiv
. (Side note: the -
at the start of the function name is a signal that this is a protocol method.) When we compare Date objects, we’re calling -equiv
:
We can see that, in the end, we’re comparing values via .valueOf
.
But, if we try to compare dates via <
or >
:
The comparison functions <
, <=
, >
, and >=
are actually macros that check types. They expect numeric values and will provide a warning if other types are given. In the end, the comparisons are simply JavaScript:
For those interested in the cljs compiler, following ::ana/numeric
to figure out how the type checks are done might be a worthwhile endeavor.