From dbcf63e85e9d19f9e3e5f468d67772cda705018a Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 11 Jun 2023 19:49:48 +0200 Subject: [PATCH] LibJS: Combine two declarative record traversals in GlobalEnvironment In GlobalEnvironment::get_binding_value(), we can avoid an extra walk of the declarative environment record if has_binding() returns a cacheable environment coordinate. --- Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp index b2fba47a28..bb56efa745 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp +++ b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp @@ -116,8 +116,11 @@ ThrowCompletionOr GlobalEnvironment::get_binding_value(VM& vm, Deprecated { // 1. Let DclRec be envRec.[[DeclarativeRecord]]. // 2. If ! DclRec.HasBinding(N) is true, then - if (MUST(m_declarative_record->has_binding(name))) { + Optional index; + if (MUST(m_declarative_record->has_binding(name, &index))) { // a. Return ? DclRec.GetBindingValue(N, S). + if (index.has_value()) + return m_declarative_record->get_binding_value_direct(vm, index.value(), strict); return m_declarative_record->get_binding_value(vm, name, strict); }