From 4cdfe684b819d2b25282cfad156669024bcba511 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Fri, 9 Dec 2022 23:41:29 +0000 Subject: [PATCH] LibJS: Remove redundant starts_with()s from is_less_than() string branch This is not in the spec and does pointless work: - If either of them is true, we would determine the same result with the manual code point iteration and comparison - If neither of them is true, we iterate from the start again and repeat the work that was just done Instead, only have the manual loop from the spec and do a length comparison at the end. Removing it brings the following microbenchmark from ~5.5s down to ~3.5s on my machine: ```js const a = "x".repeat(100_000_000) + "a"; const b = "x".repeat(100_000_000) + "b"; a < b ``` --- Userland/Libraries/LibJS/Runtime/Value.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 8244f92ef3..159421fcd4 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -1568,11 +1568,6 @@ ThrowCompletionOr is_less_than(VM& vm, Value lhs, Value rhs, bool left Utf8View x_code_points { x_string }; Utf8View y_code_points { y_string }; - if (x_code_points.starts_with(y_code_points)) - return TriState::False; - if (y_code_points.starts_with(x_code_points)) - return TriState::True; - for (auto k = x_code_points.begin(), l = y_code_points.begin(); k != x_code_points.end() && l != y_code_points.end(); ++k, ++l) { @@ -1584,7 +1579,10 @@ ThrowCompletionOr is_less_than(VM& vm, Value lhs, Value rhs, bool left } } } - VERIFY_NOT_REACHED(); + + return x_code_points.length() < y_code_points.length() + ? TriState::True + : TriState::False; } if (x_primitive.is_bigint() && y_primitive.is_string()) {