Skip to content

Commit db933a1

Browse files
committed
perf(intl): cache DateTimeFormatter on DateTimeFormat struct
1 parent ed1dcc2 commit db933a1

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,6 +79,7 @@ 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 ?
@@ -88,6 +89,7 @@ pub(crate) struct DateTimeFormat {
8889
time_style: Option<TimeStyle>,
8990
time_zone: FormatTimeZone,
9091
fieldset: CompositeFieldSet,
92+
formatter: DateTimeFormatter<CompositeFieldSet>,
9193
bound_format: Option<JsFunction>,
9294
}
9395

@@ -268,15 +270,7 @@ impl DateTimeFormat {
268270
// information about the specified calendar.
269271
let fields = ToLocalTime::from_local_epoch_milliseconds(tz)?;
270272

271-
let formatter = DateTimeFormatter::try_new_with_buffer_provider(
272-
context.intl_provider().erased_provider(),
273-
dtf.borrow().data().locale.clone().into(),
274-
dtf.borrow().data().fieldset,
275-
)
276-
.map_err(|e| {
277-
JsNativeError::range()
278-
.with_message(format!("failed to load formatter: {e}"))
279-
})?;
273+
let formatter = dtf.borrow().data().formatter.clone();
280274

281275
let dt = fields.to_formattable_datetime();
282276
let tz_info = dtf.borrow().data().time_zone.to_time_zone_info();
@@ -785,6 +779,13 @@ fn create_date_time_format(
785779
// 33. If bestFormat has a field [[hour]], then
786780
// a. Set dateTimeFormat.[[HourCycle]] to hc.
787781
// 34. Return dateTimeFormat.
782+
let formatter = DateTimeFormatter::try_new_with_buffer_provider(
783+
context.intl_provider().erased_provider(),
784+
resolved_locale.clone().into(),
785+
fieldset,
786+
)
787+
.map_err(|e| JsNativeError::range().with_message(format!("failed to load formatter: {e}")))?;
788+
788789
Ok(JsObject::from_proto_and_data(
789790
prototype,
790791
DateTimeFormat {
@@ -796,6 +797,7 @@ fn create_date_time_format(
796797
time_style,
797798
time_zone,
798799
fieldset,
800+
formatter,
799801
bound_format: None,
800802
},
801803
))

0 commit comments

Comments
 (0)