Skip to content

Commit 5718046

Browse files
committed
perf(intl): cache DateTimeFormatter on DateTimeFormat struct
1 parent 0a5235f commit 5718046

File tree

1 file changed

+11
-9
lines changed
  • core/engine/src/builtins/intl/date_time_format

1 file changed

+11
-9
lines changed

core/engine/src/builtins/intl/date_time_format/mod.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,13 @@ impl FormatTimeZone {
7979
/// JavaScript `Intl.DateTimeFormat` object.
8080
#[derive(Debug, Clone, Trace, Finalize, JsData)]
8181
#[boa_gc(unsafe_empty_trace)] // Safety: No traceable types
82+
#[allow(dead_code)]
8283
pub(crate) struct DateTimeFormat {
8384
locale: Locale,
8485
_calendar_algorithm: Option<CalendarAlgorithm>, // TODO: Potentially remove ?
8586
time_zone: FormatTimeZone,
8687
fieldset: CompositeFieldSet,
88+
formatter: DateTimeFormatter<CompositeFieldSet>,
8789
bound_format: Option<JsFunction>,
8890
}
8991

@@ -263,15 +265,7 @@ impl DateTimeFormat {
263265
// information about the specified calendar.
264266
let fields = ToLocalTime::from_local_epoch_milliseconds(tz)?;
265267

266-
let formatter = DateTimeFormatter::try_new_with_buffer_provider(
267-
context.intl_provider().erased_provider(),
268-
dtf.borrow().data().locale.clone().into(),
269-
dtf.borrow().data().fieldset,
270-
)
271-
.map_err(|e| {
272-
JsNativeError::range()
273-
.with_message(format!("failed to load formatter: {e}"))
274-
})?;
268+
let formatter = dtf.borrow().data().formatter.clone();
275269

276270
let dt = fields.to_formattable_datetime();
277271
let tz_info = dtf.borrow().data().time_zone.to_time_zone_info();
@@ -626,13 +620,21 @@ fn create_date_time_format(
626620
// 33. If bestFormat has a field [[hour]], then
627621
// a. Set dateTimeFormat.[[HourCycle]] to hc.
628622
// 34. Return dateTimeFormat.
623+
let formatter = DateTimeFormatter::try_new_with_buffer_provider(
624+
context.intl_provider().erased_provider(),
625+
resolved_locale.clone().into(),
626+
fieldset,
627+
)
628+
.map_err(|e| JsNativeError::range().with_message(format!("failed to load formatter: {e}")))?;
629+
629630
Ok(JsObject::from_proto_and_data(
630631
prototype,
631632
DateTimeFormat {
632633
locale: resolved_locale,
633634
_calendar_algorithm: intl_options.preferences.calendar_algorithm,
634635
time_zone,
635636
fieldset,
637+
formatter,
636638
bound_format: None,
637639
},
638640
))

0 commit comments

Comments
 (0)