Skip to content

Commit

Permalink
Short-circuit common case of comparing a calendar or time zone to itself
Browse files Browse the repository at this point in the history
In CalendarEquals, TimeZoneEquals, and ConsolidateCalendars, remove the
observable toString() calls from the path where both calendars are the
same object.

See: #1425
  • Loading branch information
ptomato committed Apr 17, 2021
1 parent 759ae68 commit aca09ca
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1869,11 +1869,13 @@ export const ES = ObjectAssign({}, ES2020, {
return cal1 < cal2 ? -1 : cal1 > cal2 ? 1 : 0;
},
CalendarEquals: (one, two) => {
if (one === two) return true;
const cal1 = ES.ToString(one);
const cal2 = ES.ToString(two);
return cal1 === cal2;
},
ConsolidateCalendars: (one, two) => {
if (one === two) return two;
const sOne = ES.ToString(one);
const sTwo = ES.ToString(two);
if (sOne === sTwo || sOne === 'iso8601') {
Expand Down Expand Up @@ -1918,6 +1920,7 @@ export const ES = ObjectAssign({}, ES2020, {
return new TemporalTimeZone(timeZone);
},
TimeZoneEquals: (one, two) => {
if (one === two) return true;
const tz1 = ES.ToString(one);
const tz2 = ES.ToString(two);
return tz1 === tz2;
Expand Down
12 changes: 12 additions & 0 deletions spec/calendar.html
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,13 @@ <h1>FormatCalendarAnnotation ( _id_, _showCalendar_ )</h1>

<emu-clause id="sec-temporal-calendarequals" aoid="CalendarEquals">
<h1>CalendarEquals ( _one_, _two_ )</h1>
<p>
The abstract operation CalendarEquals takes two arguments _one_ and _two_, which must be Objects.
It returns *true* if its arguments represent the same calendar.
It performs the following steps:
</p>
<emu-alg>
1. If _one_ and _two_ are the same Object value, return *true*.
1. Let _calendarOne_ be ? ToString(_one_).
1. Let _calendarTwo_ be ? ToString(_two_).
1. If _calendarOne_ is _calendarTwo_, return *true*.
Expand All @@ -338,7 +344,13 @@ <h1>CalendarEquals ( _one_, _two_ )</h1>

<emu-clause id="sec-temporal-consolidatecalendars" aoid="ConsolidateCalendars">
<h1>ConsolidateCalendars ( _one_, _two_ )</h1>
<p>
The abstract operation ConsolidateCalendars takes two arguments _one_ and _two_, which must be Objects.
It returns the calendar that should take priority when combining two Temporal objects with different calendars, or throws an exception if the calendars cannot be combined.
It performs the following steps:
</p>
<emu-alg>
1. If _one_ and _two_ are the same Object value, return _two_.
1. Let _calendarOne_ be ? ToString(_one_).
1. Let _calendarTwo_ be ? ToString(_two_).
1. If _calendarOne_ is _calendarTwo_, return _two_.
Expand Down
6 changes: 4 additions & 2 deletions spec/timezone.html
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,12 @@ <h1>GetPossibleInstantsFor ( _timeZone_, _dateTime_ )</h1>
<emu-clause id="sec-temporal-timezoneequals" aoid="TimeZoneEquals">
<h1>TimeZoneEquals ( _one_, _two_ )</h1>
<p>
The abstract operation TimeZoneEquals returns *true* if the results of
calling `toString()` on its arguments are equal.
The abstract operation TimeZoneEquals takes two arguments _one_ and _two_, which must be Objects.
It returns *true* if its arguments represent the same time zone.
It performs the following steps:
</p>
<emu-alg>
1. If _one_ and _two_ are the same Object value, return *true*.
1. Let _timeZoneOne_ be ? ToString(_one_).
1. Let _timeZoneTwo_ be ? ToString(_two_).
1. If _timeZoneOne_ is _timeZoneTwo_, return *true*.
Expand Down

0 comments on commit aca09ca

Please sign in to comment.