Repository navigation
fix: compare Temporal values by toString instead of throwing (#139) - #160
Merged
Merged
Conversation
🦋 Changeset detectedLatest commit: 588fbf3 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
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).
Merged
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>
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #139.
isEqualthrew aTypeErrorwhen comparing two different Temporal values (PlainTime,PlainDate,Instant,ZonedDateTime,Duration, ...), including values nested in objects or arrays. Temporal'svalueOf()throws by design, and ourvalueOfcheck ran before thetoString()fallback, which was never reached.Change
In the existing
// START: Modificationsblock, Temporal values skipvalueOf(). Every type exceptDurationis compared with its ownequals().Durationhas noequals(), so it's compared field by field:durationFieldsis a module-level list of the 10Durationfields (years...nanoseconds). Any otherTemporal.*tag withoutequals()falls through to the existingtoString()and key comparison.Date, boxed primitives and othervalueOfoverrides behave as before.Decisions
Symbol.toStringTagviaObject.prototype.toString. It works across realms (iframes) and with polyfills, and it's ES5 with no reference to the globalTemporal. It only runs for objects that overridevalueOf, so plain objects, arrays, Maps, Sets and typed arrays don't pay for it. Onlyais checked: past the constructor check,bhas the same type.instanceofagainst all 8 constructors (more code, fails across realms and with polyfills).valueOf()(hides other throwingvalueOfs and puts a try/catch on the hot path).toStringcheck beforevalueOf.Date#toString()drops milliseconds, so two dates 1 ms apart would compare equal.equals(), nottoString(). The first version of this PR usedtoString()for every type. Review found that the defaulttoString()of ISOPlainYearMonthandPlainMonthDayomits the reference day/year, sonew Temporal.PlainYearMonth(2026, 11, 'iso8601', 1)and(..., 2)compared equal whileequals()says they differ.equals()is Temporal's own definition of equality, so we don't depend on what each type's string includes.[Asia/Kolkata]and[Asia/Calcutta], matchingequals().toString()(Node 26,ZonedDateTimebeing the slowest by string).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 exactTemporal.Durationtag only.Durationhas noequals(), andcompare()is by length (and needsrelativeTofor calendar units).toString()balances sub-second units, so{ milliseconds: 1000 }and{ seconds: 1 }are bothPT1S, 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.Temporal.*-tagged object withoutequals()(forged tag, unusual polyfill, future type) doesn't compare 10undefinedfields and come out equal.toString()plus the three sub-second fields. It's about 30 B smaller and correct, but only because of howDuration#toString()formats values; the plain field loop is correct by inspection.skiponly whereTemporalis missing. That avoids a new devDependency and tests the engines users run on.Tests
temporalfixture group (25 cases): equal and unequal values for all 8 types, calendars, time zones and time zone aliases,PlainYearMonth/PlainMonthDayreference dates,Durations that share a string (1000 ms vs 1 s, 1000 ns vs 1 µs), cross-type (PlainDatevsPlainDateTime), Temporal vs string, nested in objects and arrays. 3.2.2 throws on most of them.custom:Dates 1 ms apart (unequal), equal boxedNumbers, unequal boxedStrings, to provevalueOfstill applies.Temporal.Fake-tagged object withoutequals(), equal and unequal, to prove only the realDurationtag gets the field loop.Bench / size
generic,es6andreactare within noise of before.customis about 18% behindpublished, but that's the new cases: 3.2.2 compares the fake Temporal objects with a plainvalueOf(), while this tree compares their keys. The real cost on existing types is oneObject.prototype.toString.callper compare of objects that overridevalueOf: about +13 ns perDatecompare in a microbenchmark (about 10 → 22 ns on Node 26). Plain-object compares are unchanged. Readinga[Symbol.toStringTag]directly was slower (about 40 ns), so I keptObject.prototype.toString.Durationfield list and loop.Other
pack-snapshot.jsonregenerated (index.jschanged); the diff is thevalueOfblock and thedurationFieldslist.index.d.tsis unchanged.