From 8fd9090cf364f3192dfe1ec08d089aaa8b2bf82a Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Wed, 4 Dec 2024 20:23:54 +0000 Subject: [PATCH] perf(transformer/class-properties): replace recursion with loop --- .../src/es2022/class_properties/private.rs | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/crates/oxc_transformer/src/es2022/class_properties/private.rs b/crates/oxc_transformer/src/es2022/class_properties/private.rs index 811ba47f7e9cce..9daf4be3842ad2 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/private.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/private.rs @@ -1216,20 +1216,35 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { /// Recursively check if the expression has optional expression. #[inline] fn has_optional_expression(expr: &Expression<'a>) -> bool { - match expr { - Expression::CallExpression(call) => { - call.optional || Self::has_optional_expression(call.callee.get_inner_expression()) - } - Expression::StaticMemberExpression(member) => { - member.optional || Self::has_optional_expression(&member.object) - } - Expression::ComputedMemberExpression(member) => { - member.optional || Self::has_optional_expression(&member.object) - } - Expression::PrivateFieldExpression(member) => { - member.optional || Self::has_optional_expression(&member.object) + let mut expr = expr; + loop { + match expr { + Expression::CallExpression(call) => { + if call.optional { + return true; + } + expr = call.callee.get_inner_expression(); + } + Expression::StaticMemberExpression(member) => { + if member.optional { + return true; + } + expr = &member.object; + } + Expression::ComputedMemberExpression(member) => { + if member.optional { + return true; + } + expr = &member.object; + } + Expression::PrivateFieldExpression(member) => { + if member.optional { + return true; + } + expr = &member.object; + } + _ => return false, } - _ => false, } }