From caf2e675bfb6e28f35b72940bd63350d2aa0ab27 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 9 May 2024 15:21:24 +0200 Subject: [PATCH] LibJS/Bytecode: Don't emit `GetGlobal undefined` We know that `undefined` in the global scope is always the proper undefined value. This commit takes advantage of that by simply emitting a constant undefined value instead. Unfortunately we can't be so sure in other scopes. --- Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index 3d795e9fbd4..d66e1af98d2 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -382,6 +382,10 @@ Bytecode::CodeGenerationErrorOr> Identifier::generate_by return local; } + if (is_global() && m_string == "undefined"sv) { + return generator.add_constant(js_undefined()); + } + auto dst = choose_dst(generator, preferred_dst); if (is_global()) { generator.emit(dst, generator.intern_identifier(m_string), generator.next_global_variable_cache());