1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:07:35 +00:00

LibJS: Add fast path for magical "length" property in LengthOfArrayLike

For Array objects, we can avoid a generic Get here since we know it has
magical "length" behavior anyway.
This commit is contained in:
Andreas Kling 2023-12-08 12:34:42 +01:00
parent 8e5f201e59
commit 73ceb475b9
2 changed files with 5 additions and 0 deletions

View file

@ -87,6 +87,10 @@ ThrowCompletionOr<NonnullGCPtr<Object>> construct_impl(VM&, FunctionObject& func
// 7.3.19 LengthOfArrayLike ( obj ), https://tc39.es/ecma262/#sec-lengthofarraylike
ThrowCompletionOr<size_t> length_of_array_like(VM& vm, Object const& object)
{
// OPTIMIZATION: For Array objects with a magical "length" property, it should always reflect the size of indexed property storage.
if (object.has_magical_length_property())
return object.indexed_properties().array_like_size();
// 1. Return (? ToLength(? Get(obj, "length"))).
return TRY(object.get(vm.names.length)).to_length(vm);
}

View file

@ -224,6 +224,7 @@ public:
static FlatPtr may_interfere_with_indexed_property_access_offset() { return OFFSET_OF(Object, m_may_interfere_with_indexed_property_access); }
static FlatPtr indexed_properties_offset() { return OFFSET_OF(Object, m_indexed_properties); }
[[nodiscard]] bool has_magical_length_property() const { return m_has_magical_length_property; }
static FlatPtr has_magical_length_property_offset() { return OFFSET_OF(Object, m_has_magical_length_property); }
[[nodiscard]] bool is_typed_array() const { return m_is_typed_array; }