From 1d4f63e4cd5e82f10cae2e0696adaa50f08a0639 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Wed, 4 Jun 2025 17:01:02 +0200 Subject: [PATCH] LibJS: Add simple storage fast path in internal_define_own_property() ...of Array. If array has simple storage, which implies that attributes of all indexed properties are default, and newly added property also has default attribute, we can do a fast path and skip lots of checks that happen in `Object::internal_define_own_property()`. --- Libraries/LibJS/Runtime/Array.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Libraries/LibJS/Runtime/Array.cpp b/Libraries/LibJS/Runtime/Array.cpp index 131d75894cd..ba8caa11762 100644 --- a/Libraries/LibJS/Runtime/Array.cpp +++ b/Libraries/LibJS/Runtime/Array.cpp @@ -390,7 +390,21 @@ ThrowCompletionOr Array::internal_define_own_property(PropertyKey const& p return false; // h. Let succeeded be ! OrdinaryDefineOwnProperty(A, P, Desc). - auto succeeded = MUST(Object::internal_define_own_property(property_key, property_descriptor, precomputed_get_own_property)); + bool succeeded = true; + auto* storage = indexed_properties().storage(); + auto attributes = property_descriptor.attributes(); + // OPTIMIZATION: Fast path for arrays with simple indexed properties storage. + if (property_descriptor.is_data_descriptor() && attributes == default_attributes && storage && storage->is_simple_storage()) { + if (!m_is_extensible) { + auto existing_descriptor = TRY(internal_get_own_property(property_key)); + if (!existing_descriptor.has_value()) + return false; + } + + storage->put(property_key.as_number(), property_descriptor.value.value()); + } else { + succeeded = MUST(Object::internal_define_own_property(property_key, property_descriptor, precomputed_get_own_property)); + } // i. If succeeded is false, return false. if (!succeeded)