LibJS: Don't convert to UTF-8 in order to compare two UTF-16 strings

If we have two PrimitiveString objects that are both backed by UTF-16
data, we don't have to convert them to UTF-8 for equality checking.
Just compare the underlying UTF-16 data. :^)
This commit is contained in:
Andreas Kling 2025-04-10 12:18:08 +02:00 committed by Andreas Kling
parent e80d1c1a86
commit d78e3590d5
Notes: github-actions[bot] 2025-04-12 09:08:53 +00:00
3 changed files with 14 additions and 1 deletions

View file

@ -106,6 +106,17 @@ Utf16View PrimitiveString::utf16_string_view() const
return m_utf16_string->view();
}
bool PrimitiveString::operator==(PrimitiveString const& other) const
{
if (this == &other)
return true;
if (m_utf8_string.has_value() && other.m_utf8_string.has_value())
return m_utf8_string->bytes_as_string_view() == other.m_utf8_string->bytes_as_string_view();
if (m_utf16_string.has_value() && other.m_utf16_string.has_value())
return m_utf16_string->string() == other.m_utf16_string->string();
return utf8_string_view() == other.utf8_string_view();
}
ThrowCompletionOr<Optional<Value>> PrimitiveString::get(VM& vm, PropertyKey const& property_key) const
{
if (property_key.is_symbol())

View file

@ -47,6 +47,8 @@ public:
ThrowCompletionOr<Optional<Value>> get(VM&, PropertyKey const&) const;
[[nodiscard]] bool operator==(PrimitiveString const&) const;
protected:
enum class RopeTag { Rope };
explicit PrimitiveString(RopeTag)

View file

@ -2225,7 +2225,7 @@ bool same_value_non_number(Value lhs, Value rhs)
// 5. If x is a String, then
if (lhs.is_string()) {
// a. If x and y are exactly the same sequence of code units (same length and same code units at corresponding indices), return true; otherwise, return false.
return lhs.as_string().utf8_string_view() == rhs.as_string().utf8_string_view();
return lhs.as_string() == rhs.as_string();
}
// 3. If x is undefined, return true.