From 9fcd6776cfdec9fa3f2fe0af4c3da89dbf48716b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 27 Jan 2024 20:37:45 +0100 Subject: [PATCH] LibJS: Add fast path for Array.length in the bytecode interpreter 13.8% speed-up on Kraken/ai-astar.js in interpreter mode. :^) --- Userland/Libraries/LibJS/Bytecode/CommonImplementations.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h index 06a8f3510e1..1795bdceeee 100644 --- a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h +++ b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h @@ -84,6 +84,11 @@ inline ThrowCompletionOr get_by_id(VM& vm, DeprecatedFlyString const& pro auto base_obj = TRY(base_object_for_get(vm, base_value)); + // OPTIMIZATION: Fast path for the magical "length" property on Array objects. + if (base_obj->has_magical_length_property() && property == vm.names.length.as_string()) { + return Value { base_obj->indexed_properties().array_like_size() }; + } + // OPTIMIZATION: If the shape of the object hasn't changed, we can use the cached property offset. auto& shape = base_obj->shape(); if (&shape == cache.shape) {