diff --git a/src/stdlib/text.rs b/src/stdlib/text.rs index c9591d6b..cdeb9098 100644 --- a/src/stdlib/text.rs +++ b/src/stdlib/text.rs @@ -279,6 +279,12 @@ pub fn native_replace(args: Vec) -> Result { let text = expect_text(&args[0])?; let old = expect_text(&args[1])?; let new = expect_text(&args[2])?; + + // Optimization: avoid string allocation if string doesn't contain old + if !text.contains(old.as_ref()) { + return Ok(Value::Text(Arc::clone(&text))); + } + let result = text.replace(old.as_ref(), new.as_ref()); Ok(Value::Text(Arc::from(result))) }