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 16, 2021
1 parent fafbd7d commit 8acf69b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1844,11 +1844,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 @@ -1893,6 +1895,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
2 changes: 2 additions & 0 deletions spec/calendar.html
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ <h1>FormatCalendarAnnotation ( _id_, _showCalendar_ )</h1>
<emu-clause id="sec-temporal-calendarequals" aoid="CalendarEquals">
<h1>CalendarEquals ( _one_, _two_ )</h1>
<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 @@ -339,6 +340,7 @@ <h1>CalendarEquals ( _one_, _two_ )</h1>
<emu-clause id="sec-temporal-consolidatecalendars" aoid="ConsolidateCalendars">
<h1>ConsolidateCalendars ( _one_, _two_ )</h1>
<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
1 change: 1 addition & 0 deletions spec/timezone.html
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ <h1>TimeZoneEquals ( _one_, _two_ )</h1>
calling `toString()` on its arguments are equal.
</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 8acf69b

Please sign in to comment.