Skip to content
8 changes: 8 additions & 0 deletions benches/scripts/intl/collator-compare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const kIterationCount = 1000;
const collator = new Intl.Collator("en");

function main() {
for (let i = 0; i < kIterationCount; i++) {
collator.compare("apple", "banana");
}
}
10 changes: 10 additions & 0 deletions benches/scripts/intl/collator-construction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const kIterationCount = 100;
const locales = ["en", "de", "ja", "ar", "fr", "es", "zh"];

function main() {
for (let i = 0; i < kIterationCount; i++) {
for (const locale of locales) {
new Intl.Collator(locale);
}
}
}
10 changes: 10 additions & 0 deletions benches/scripts/intl/datetimeformat-construction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const kIterationCount = 100;
const locales = ["en", "de", "ja", "ar", "fr", "es", "zh"];

function main() {
for (let i = 0; i < kIterationCount; i++) {
for (const locale of locales) {
new Intl.DateTimeFormat(locale);
}
}
}
9 changes: 9 additions & 0 deletions benches/scripts/intl/datetimeformat-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const kIterationCount = 1000;
const dtf = new Intl.DateTimeFormat("en");
const date = new Date(2024, 0, 1);

function main() {
for (let i = 0; i < kIterationCount; i++) {
dtf.format(date);
}
}
16 changes: 16 additions & 0 deletions benches/scripts/intl/datetimeformat-with-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const kIterationCount = 100;

function main() {
for (let i = 0; i < kIterationCount; i++) {
new Intl.DateTimeFormat("en", {
year: "numeric",
month: "long",
day: "numeric",
});
new Intl.DateTimeFormat("en", {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
});
}
}
10 changes: 10 additions & 0 deletions benches/scripts/intl/listformat-construction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const kIterationCount = 100;
const locales = ["en", "de", "ja", "ar", "fr", "es", "zh"];

function main() {
for (let i = 0; i < kIterationCount; i++) {
for (const locale of locales) {
new Intl.ListFormat(locale);
}
}
}
9 changes: 9 additions & 0 deletions benches/scripts/intl/listformat-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const kIterationCount = 1000;
const lf = new Intl.ListFormat("en");
const list = ["apple", "banana", "cherry"];

function main() {
for (let i = 0; i < kIterationCount; i++) {
lf.format(list);
}
}
10 changes: 10 additions & 0 deletions benches/scripts/intl/numberformat-construction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const kIterationCount = 100;
const locales = ["en", "de", "ja", "ar", "fr", "es", "zh"];

function main() {
for (let i = 0; i < kIterationCount; i++) {
for (const locale of locales) {
new Intl.NumberFormat(locale);
}
}
}
9 changes: 9 additions & 0 deletions benches/scripts/intl/numberformat-different-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const kIterationCount = 100;

function main() {
for (let i = 0; i < kIterationCount; i++) {
new Intl.NumberFormat("en", { style: "currency", currency: "USD" });
new Intl.NumberFormat("en", { style: "percent" });
new Intl.NumberFormat("en", { notation: "scientific" });
}
}
10 changes: 10 additions & 0 deletions benches/scripts/intl/pluralrules-construction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const kIterationCount = 100;
const locales = ["en", "de", "ja", "ar", "fr", "es", "zh"];

function main() {
for (let i = 0; i < kIterationCount; i++) {
for (const locale of locales) {
new Intl.PluralRules(locale);
}
}
}
8 changes: 8 additions & 0 deletions benches/scripts/intl/pluralrules-select.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const kIterationCount = 1000;
const pr = new Intl.PluralRules("en");

function main() {
for (let i = 0; i < kIterationCount; i++) {
pr.select(i);
}
}
10 changes: 10 additions & 0 deletions benches/scripts/intl/segmenter-construction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const kIterationCount = 100;
const locales = ["en", "de", "ja", "ar", "fr", "es", "zh"];

function main() {
for (let i = 0; i < kIterationCount; i++) {
for (const locale of locales) {
new Intl.Segmenter(locale);
}
}
}
9 changes: 9 additions & 0 deletions benches/scripts/intl/segmenter-segment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const kIterationCount = 100;
const segmenter = new Intl.Segmenter("en");
const text = "The quick brown fox jumps over the lazy dog.";

function main() {
for (let i = 0; i < kIterationCount; i++) {
[...segmenter.segment(text)];
}
}
20 changes: 11 additions & 9 deletions core/engine/src/builtins/intl/date_time_format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl FormatTimeZone {
/// JavaScript `Intl.DateTimeFormat` object.
#[derive(Debug, Clone, Trace, Finalize, JsData)]
#[boa_gc(unsafe_empty_trace)] // Safety: No traceable types
#[allow(dead_code)]
pub(crate) struct DateTimeFormat {
locale: Locale,
calendar_algorithm: Option<CalendarAlgorithm>, // TODO: Potentially remove ?
Expand All @@ -88,6 +89,7 @@ pub(crate) struct DateTimeFormat {
time_style: Option<TimeStyle>,
time_zone: FormatTimeZone,
fieldset: CompositeFieldSet,
formatter: DateTimeFormatter<CompositeFieldSet>,
bound_format: Option<JsFunction>,
}

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

let formatter = DateTimeFormatter::try_new_with_buffer_provider(
context.intl_provider().erased_provider(),
dtf.borrow().data().locale.clone().into(),
dtf.borrow().data().fieldset,
)
.map_err(|e| {
JsNativeError::range()
.with_message(format!("failed to load formatter: {e}"))
})?;
let formatter = dtf.borrow().data().formatter.clone();

let dt = fields.to_formattable_datetime();
let tz_info = dtf.borrow().data().time_zone.to_time_zone_info();
Expand Down Expand Up @@ -785,6 +779,13 @@ fn create_date_time_format(
// 33. If bestFormat has a field [[hour]], then
// a. Set dateTimeFormat.[[HourCycle]] to hc.
// 34. Return dateTimeFormat.
let formatter = DateTimeFormatter::try_new_with_buffer_provider(
context.intl_provider().erased_provider(),
resolved_locale.clone().into(),
fieldset,
)
.map_err(|e| JsNativeError::range().with_message(format!("failed to load formatter: {e}")))?;

Ok(JsObject::from_proto_and_data(
prototype,
DateTimeFormat {
Expand All @@ -796,6 +797,7 @@ fn create_date_time_format(
time_style,
time_zone,
fieldset,
formatter,
bound_format: None,
},
))
Expand Down
Loading