From 896f8be09f2abb490d0a8e391af38fbd6e54d7dd Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 19 Mar 2025 10:38:22 -0500 Subject: [PATCH] LibJS: Add fast path for strings in Value::to_property_key() If the Value is already a primitive string, we can skip all the conversion ceremony and return a PropertyKey right away. --- Libraries/LibJS/Runtime/Value.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp index 50f0ac2f826..6da7a6b7ad8 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -898,6 +898,10 @@ ThrowCompletionOr Value::to_property_key(VM& vm) const if (is_int32() && as_i32() >= 0) return PropertyKey { as_i32() }; + // OPTIMIZATION: If this is already a string, we can skip all the ceremony. + if (is_string()) + return PropertyKey { as_string().utf8_string() }; + // 1. Let key be ? ToPrimitive(argument, string). auto key = TRY(to_primitive(vm, PreferredType::String));