Skip to content

fix: compare Temporal values by toString instead of throwing (#139) - #160

Merged
ryan-roemer merged 4 commits into
masterfrom
fix/temporal
Nov 20, 2026
Merged

ryan-roemer merged 4 commits into
masterfrom
fix/temporal

Conversation

@ryan-roemer

@ryan-roemer ryan-roemer commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

Fixes #139.

isEqual threw a TypeError when comparing two different Temporal values (PlainTime, PlainDate, Instant, ZonedDateTime, Duration, ...), including values nested in objects or arrays. Temporal's valueOf() throws by design, and our valueOf check ran before the toString() fallback, which was never reached.

Change

In the existing // START: Modifications block, Temporal values skip valueOf(). Every type except Duration is compared with its own equals(). Duration has no equals(), so it's compared field by field:

var tag;
if (a.valueOf !== Object.prototype.valueOf && typeof a.valueOf === 'function' && typeof b.valueOf === 'function') {
  tag = Object.prototype.toString.call(a);
  if (tag.indexOf('[object Temporal.') !== 0)
    return a.valueOf() === b.valueOf();
  if (typeof a.equals === 'function') return a.equals(b);
  if (tag === '[object Temporal.Duration]') {
    for (i = durationFields.length; i-- !== 0;)
      if (a[durationFields[i]] !== b[durationFields[i]]) return false;
    return true;
  }
}

durationFields is a module-level list of the 10 Duration fields (years ... nanoseconds). Any other Temporal.* tag without equals() falls through to the existing toString() and key comparison. Date, boxed primitives and other valueOf overrides behave as before.

Decisions

  • Detection: Symbol.toStringTag via Object.prototype.toString. It works across realms (iframes) and with polyfills, and it's ES5 with no reference to the global Temporal. It only runs for objects that override valueOf, so plain objects, arrays, Maps, Sets and typed arrays don't pay for it. Only a is checked: past the constructor check, b has the same type.
    • Rejected: instanceof against all 8 constructors (more code, fails across realms and with polyfills).
    • Rejected: try/catch around valueOf() (hides other throwing valueOfs and puts a try/catch on the hot path).
    • Rejected: moving the toString check before valueOf. Date#toString() drops milliseconds, so two dates 1 ms apart would compare equal.
  • Comparison: equals(), not toString(). The first version of this PR used toString() for every type. Review found that the default toString() of ISO PlainYearMonth and PlainMonthDay omits the reference day/year, so new Temporal.PlainYearMonth(2026, 11, 'iso8601', 1) and (..., 2) compared equal while equals() says they differ. equals() is Temporal's own definition of equality, so we don't depend on what each type's string includes.
    • It also treats time zone aliases as equal: [Asia/Kolkata] and [Asia/Calcutta], matching equals().
    • It's faster: about 95 ns per compare vs. 150–690 ns for toString() (Node 26, ZonedDateTime being the slowest by string).
    • Rejected: toString({ calendarName: 'always' }). It keeps the reference date, but it's slower still and stays tied to each type's serialization.
  • Duration: field by field, for the exact Temporal.Duration tag only. Duration has no equals(), and compare() is by length (and needs relativeTo for calendar units). toString() balances sub-second units, so { milliseconds: 1000 } and { seconds: 1 } are both PT1S, yet they render differently (toLocaleString(): "1,000 milliseconds" vs "1 second"). Comparing all 10 fields treats them as different, like any other object whose fields differ.
    • The loop is gated on the exact tag so that any other Temporal.*-tagged object without equals() (forged tag, unusual polyfill, future type) doesn't compare 10 undefined fields and come out equal.
    • Rejected: toString() plus the three sub-second fields. It's about 30 B smaller and correct, but only because of how Duration#toString() formats values; the plain field loop is correct by inspection.
  • Tests: skip, no polyfill. Node 26, Chromium 156, Firefox 157 and WebKit 27.2 all have Temporal built in, so nothing is skipped today. Cases are marked skip only where Temporal is missing. That avoids a new devDependency and tests the engines users run on.

Tests

  • New temporal fixture group (25 cases): equal and unequal values for all 8 types, calendars, time zones and time zone aliases, PlainYearMonth/PlainMonthDay reference dates, Durations that share a string (1000 ms vs 1 s, 1000 ns vs 1 µs), cross-type (PlainDate vs PlainDateTime), Temporal vs string, nested in objects and arrays. 3.2.2 throws on most of them.
  • custom:
    • Dates 1 ms apart (unequal), equal boxed Numbers, unequal boxed Strings, to prove valueOf still applies.
    • A fake Temporal.Fake-tagged object without equals(), equal and unequal, to prove only the real Duration tag gets the field loop.
  • No bench group for Temporal: 3.2.2 and fast-deep-equal both throw, which leaves one candidate.

Bench / size

  • Bench: generic, es6 and react are within noise of before. custom is about 18% behind published, but that's the new cases: 3.2.2 compares the fake Temporal objects with a plain valueOf(), while this tree compares their keys. The real cost on existing types is one Object.prototype.toString.call per compare of objects that override valueOf: about +13 ns per Date compare in a microbenchmark (about 10 → 22 ns on Node 26). Plain-object compares are unchanged. Reading a[Symbol.toStringTag] directly was slower (about 40 ns), so I kept Object.prototype.toString.
  • Size: min+gz 702 → 830 B (+128). Most of that is the Duration field list and loop.

Other

  • pack-snapshot.json regenerated (index.js changed); the diff is the valueOf block and the durationFields list.
  • Patch changeset. index.d.ts is unchanged.
  • README: size claim "under 660 bytes" → "under 850 bytes". It was already stale at 702 B in 3.2.3; this PR makes it 830 B.

@changeset-bot

changeset-bot Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 588fbf3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
react-fast-compare Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ryan-roemer

Copy link
Copy Markdown
Member Author

@MartinCernikSG -- have a patch up here for Temporal. Can you take a look and try it out if you get a moment? Thanks!

Review follow-up for #160. The default toString() of ISO
PlainYearMonth and PlainMonthDay omits the reference day/year, so two
values that equals() calls different compared equal.

- Temporal values now use equals(). Duration has none and falls
  through to the existing toString() check.
- equals() also treats time zone aliases (Asia/Kolkata, Asia/Calcutta)
  as equal, and runs about 1.5-7x faster than toString().
- Fixtures: reference day/year and time zone alias cases.
- pack-snapshot.json regenerated. Changeset text updated.

Size: min+gz 729 -> 746 B (+17; +44 vs. 3.2.3).
Review follow-up for #160. Duration has no equals(), and its toString()
balances sub-second units, so 1000 ms and 1 s (both PT1S) compared
equal although their fields and toLocaleString() output differ.

- Compare all 10 Duration fields instead of toString().
- Fixtures: 1000 ms vs 1 s, 1000 ns vs 1 us, equal sub-second
  durations.
- pack-snapshot.json regenerated. Changeset text updated.
- README: size claim updated to under 850 bytes.

Size: min+gz 746 -> 809 B (+63; +107 vs. 3.2.3).
Review follow-up for #160. Any object tagged Temporal.* that overrides
valueOf and has no equals() reached the duration field loop, where all
10 fields are undefined, so it always compared equal.

- Run the field loop only for an exact Temporal.Duration tag. Other
  Temporal.* tags without equals() fall through to the toString() and
  key comparison.
- Fixtures: a fake Temporal.Fake-tagged object, equal and unequal.
- pack-snapshot.json regenerated.

Size: min+gz 809 -> 830 B (+21; +128 vs. 3.2.3).
@ryan-roemer
ryan-roemer merged commit 46180a6 into master Nov 20, 2026
5 checks passed
@ryan-roemer
ryan-roemer deleted the fix/temporal branch November 20, 2026 18:05
@github-actions github-actions Bot mentioned this pull request Nov 20, 2026
ryan-roemer pushed a commit that referenced this pull request Nov 20, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to master, this PR
will be updated.


# Releases
## react-fast-compare@3.2.4

### Patch Changes

- [#160](#160)
[`46180a6`](46180a6)
- Fix `isEqual` throwing when comparing two different `Temporal` values
(e.g. `Temporal.PlainTime`), including ones nested in objects or arrays.
They're now compared with their `equals()` method, and
`Temporal.Duration`, which has none, field by field.

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

isEqual throws on Temporal values because it calls valueOf

1 participant