From 7af7e90e3f148954b77e07d05bf679fab9ab3bba Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Thu, 13 Jul 2023 01:44:48 +0200 Subject: [PATCH] LibJS: Speed up has declaration check in ScopePusher This change speeds up the process of checking variable declaration within the scope by replacing the iteration through all declared variables (without the ability to exit early from the loop) with hash map lookups. This change cuts google maps loading by 17s on my computer. --- Userland/Libraries/LibJS/Parser.cpp | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/Userland/Libraries/LibJS/Parser.cpp b/Userland/Libraries/LibJS/Parser.cpp index 4b00bc3f8d..9dfadc139c 100644 --- a/Userland/Libraries/LibJS/Parser.cpp +++ b/Userland/Libraries/LibJS/Parser.cpp @@ -299,25 +299,10 @@ public: } bool scope_has_declaration = false; - MUST(m_node->for_each_var_declared_name([&](auto const& name) { - if (m_function_parameters.has_value() && m_forbidden_lexical_names.contains(name)) - return; - if (name == identifier_group_name) - scope_has_declaration = true; - })); - MUST(m_node->for_each_lexically_declared_name([&](auto const& name) { - if (name == identifier_group_name) - scope_has_declaration = true; - })); - - MUST(m_node->for_each_var_function_declaration_in_reverse_order([&](auto const& declaration) { - if (declaration.name() == identifier_group_name) - scope_has_declaration = true; - })); - MUST(m_node->for_each_lexical_function_declaration_in_reverse_order([&](auto const& declaration) { - if (declaration.name() == identifier_group_name) - scope_has_declaration = true; - })); + if (is_top_level() && m_var_names.contains(identifier_group_name)) + scope_has_declaration = true; + else if (m_lexical_names.contains(identifier_group_name) || m_function_names.contains(identifier_group_name)) + scope_has_declaration = true; bool hoistable_function_declaration = false; for (auto const& function_declaration : m_functions_to_hoist) {