From d5222e9bba86cb1d1c7f76641feff727df865afb Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 8 Apr 2025 13:39:21 -0400 Subject: [PATCH] LibJS: Do not generate a format string in Number.prototype.toFixed We can use the placeholder syntax to specify the precision dynamically. Note that `fraction_digits` is a double, which we do not support as a precision argument. It's safe to cast to an integer here because we guaranteed above that the value is in the range [0, 100], and is not fractional. --- Libraries/LibJS/Runtime/NumberPrototype.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Libraries/LibJS/Runtime/NumberPrototype.cpp b/Libraries/LibJS/Runtime/NumberPrototype.cpp index ecdb66f3e5a..a4dad6522fc 100644 --- a/Libraries/LibJS/Runtime/NumberPrototype.cpp +++ b/Libraries/LibJS/Runtime/NumberPrototype.cpp @@ -256,8 +256,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_fixed) // `number` double. Instead of generating a huge, unwieldy `n`, we format // the double using our existing formatting code. - auto number_format_string = ByteString::formatted("{{}}{{:.{}f}}", fraction_digits); - return PrimitiveString::create(vm, ByteString::formatted(number_format_string, s, number)); + return PrimitiveString::create(vm, MUST(String::formatted("{}{:.{}f}", s, number, static_cast(fraction_digits)))); } // 20.2.1 Number.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sup-number.prototype.tolocalestring