Skip to content

Commit 49d31e3

Browse files
committed
Save String constructor at startup
This commit aligns wtih the pattern we use for the Number constructor and other ES built-in objects, where the object is saved when the ecmascript.ts module is imported and that saved reference is used in place of `String()` elsewhere.
1 parent 4ec6568 commit 49d31e3

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

lib/ecmascript.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const MathTrunc = Math.trunc;
99
const NumberIsNaN = Number.isNaN;
1010
const NumberIsFinite = Number.isFinite;
1111
const NumberCtor = Number;
12+
const StringCtor = String;
1213
const NumberMaxSafeInteger = Number.MAX_SAFE_INTEGER;
1314
const ObjectCreate = Object.create;
1415
const ObjectDefineProperty = Object.defineProperty;
@@ -96,7 +97,7 @@ export function ToString(value: unknown): string {
9697
if (typeof value === 'symbol') {
9798
throw new TypeError('Cannot convert a Symbol value to a String');
9899
}
99-
return String(value);
100+
return StringCtor(value);
100101
}
101102

102103
export function ToIntegerThrowOnInfinity(value): number {
@@ -188,7 +189,7 @@ function getIntlDateTimeFormatEnUsForTimeZone(timeZoneIdentifier) {
188189
let instance = IntlDateTimeFormatEnUsCache.get(timeZoneIdentifier);
189190
if (instance === undefined) {
190191
instance = new IntlDateTimeFormat('en-us', {
191-
timeZone: String(timeZoneIdentifier),
192+
timeZone: StringCtor(timeZoneIdentifier),
192193
hour12: false,
193194
era: 'short',
194195
year: 'numeric',
@@ -2225,7 +2226,7 @@ export function TemporalZonedDateTimeToString(
22252226
}
22262227

22272228
export function ParseOffsetString(string) {
2228-
const match = OFFSET.exec(String(string));
2229+
const match = OFFSET.exec(StringCtor(string));
22292230
if (!match) return null;
22302231
const sign = match[1] === '-' || match[1] === '\u2212' ? -1 : +1;
22312232
const hours = +match[2];
@@ -2238,7 +2239,7 @@ export function ParseOffsetString(string) {
22382239
export function GetCanonicalTimeZoneIdentifier(timeZoneIdentifier) {
22392240
const offsetNs = ParseOffsetString(timeZoneIdentifier);
22402241
if (offsetNs !== null) return FormatTimeZoneOffsetString(offsetNs);
2241-
const formatter = getIntlDateTimeFormatEnUsForTimeZone(String(timeZoneIdentifier));
2242+
const formatter = getIntlDateTimeFormatEnUsForTimeZone(StringCtor(timeZoneIdentifier));
22422243
return formatter.resolvedOptions().timeZone;
22432244
}
22442245

lib/timezone.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class TimeZone implements Temporal.TimeZone {
139139
}
140140
toString() {
141141
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
142-
return String(GetSlot(this, TIMEZONE_ID));
142+
return ES.ToString(GetSlot(this, TIMEZONE_ID));
143143
}
144144
toJSON() {
145145
return ES.ToString(this);

0 commit comments

Comments
 (0)