diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index 556a0eef419..8a6af4cf0d3 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -269,6 +269,14 @@ Bytecode::CodeGenerationErrorOr> LogicalExpression::gene Bytecode::CodeGenerationErrorOr> UnaryExpression::generate_bytecode(Bytecode::Generator& generator, Optional preferred_dst) const { Bytecode::Generator::SourceLocationScope scope(generator, *this); + + // OPTIMIZATION: Turn expressions like `-1` into a constant. + if (m_op == UnaryOp::Minus && is(*m_lhs)) { + auto& numeric_literal = static_cast(*m_lhs); + auto value = numeric_literal.value(); + return generator.add_constant(Value(-value.as_double())); + } + if (m_op == UnaryOp::Delete) return generator.emit_delete_reference(m_lhs);