mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-10-03 06:39:07 +00:00
LibJS: Avoid string trimming in GlobalObject.parseInt() when possible
In the common case, parseInt() is being called with strings that don't have leading whitespace. By checking for that first, we can avoid the cost of trimming and multiple malloc/GC allocations.
This commit is contained in:
parent
0f70ff9a67
commit
3170ad2ee3
Notes:
sideshowbarker
2024-07-17 21:16:31 +09:00
Author: https://github.com/awesomekling
Commit: 3170ad2ee3
Pull-request: https://github.com/SerenityOS/serenity/pull/24260
Reviewed-by: https://github.com/Hendiadyoin1
Reviewed-by: https://github.com/mattco98
1 changed files with 7 additions and 1 deletions
|
@ -303,7 +303,13 @@ JS_DEFINE_NATIVE_FUNCTION(GlobalObject::parse_int)
|
|||
auto input_string = TRY(string.to_string(vm));
|
||||
|
||||
// 2. Let S be ! TrimString(inputString, start).
|
||||
auto trimmed_string = MUST(trim_string(vm, PrimitiveString::create(vm, move(input_string)), TrimMode::Left));
|
||||
String trimmed_string;
|
||||
// OPTIMIZATION: We can skip the trimming step when the value already starts with an alphanumeric ASCII character.
|
||||
if (input_string.is_empty() || is_ascii_alphanumeric(input_string.bytes_as_string_view()[0])) {
|
||||
trimmed_string = input_string;
|
||||
} else {
|
||||
trimmed_string = MUST(trim_string(vm, PrimitiveString::create(vm, move(input_string)), TrimMode::Left));
|
||||
}
|
||||
|
||||
// 3. Let sign be 1.
|
||||
auto sign = 1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue