Skip to content

Commit 677fd93

Browse files
authored
perf: avoid unnecessary calls of to_string in cow_* (#4166)
1 parent f0abe2c commit 677fd93

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

core/engine/src/builtins/number/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -916,10 +916,11 @@ impl Number {
916916

917917
/// Helper function that formats a float as a ES6-style exponential number string.
918918
fn f64_to_exponential(n: f64) -> JsString {
919-
js_string!(match n.abs() {
920-
x if x >= 1.0 || x == 0.0 => format!("{n:e}").cow_replace('e', "e+").to_string(),
921-
_ => format!("{n:e}"),
922-
})
919+
let s = format!("{n:e}");
920+
match n.abs() {
921+
x if x >= 1.0 || x == 0.0 => js_string!(s.cow_replace('e', "e+")),
922+
_ => js_string!(s),
923+
}
923924
}
924925

925926
/// Helper function that formats a float as a ES6-style exponential number string with a given precision.

core/engine/src/builtins/string/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ use boa_macros::utf16;
2727
use boa_profiler::Profiler;
2828
use cow_utils::CowUtils;
2929
use icu_normalizer::{ComposingNormalizer, DecomposingNormalizer};
30-
use std::cmp::{max, min};
30+
use std::{
31+
borrow::Cow,
32+
cmp::{max, min},
33+
};
3134

3235
use super::{BuiltInBuilder, BuiltInConstructor, IntrinsicObject};
3336

@@ -1727,16 +1730,20 @@ impl String {
17271730
// 4. Let upperText be the result of toUppercase(sText), according to
17281731
// the Unicode Default Case Conversion algorithm.
17291732
let text = string.map_valid_segments(|s| {
1730-
if UPPER {
1731-
s.cow_to_uppercase().to_string()
1733+
let cow_str = if UPPER {
1734+
s.cow_to_uppercase()
17321735
} else {
1733-
s.cow_to_lowercase().to_string()
1736+
s.cow_to_lowercase()
1737+
};
1738+
match cow_str {
1739+
Cow::Borrowed(_) => s,
1740+
Cow::Owned(replaced_str) => replaced_str,
17341741
}
17351742
});
17361743

17371744
// 5. Let L be ! CodePointsToString(upperText).
17381745
// 6. Return L.
1739-
Ok(js_string!(text).into())
1746+
Ok(text.into())
17401747
}
17411748

17421749
/// [`String.prototype.toLocaleLowerCase ( [ locales ] )`][lower] and

core/engine/src/builtins/temporal/zoneddatetime/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ impl ZonedDateTime {
478478

479479
let era = zdt.inner.era_with_provider(context.tz_provider())?;
480480
Ok(era
481-
.map(|tinystr| JsString::from(tinystr.cow_to_lowercase().to_string()))
481+
.map(|tinystr| JsString::from(tinystr.cow_to_lowercase()))
482482
.into_or_undefined())
483483
}
484484

0 commit comments

Comments
 (0)