summaryrefslogtreecommitdiff
path: root/ext/node_resolver/analyze.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-08-26 11:13:39 -0400
committerGitHub <noreply@github.com>2024-08-26 11:13:39 -0400
commitd8dfe6dc97942a2e67c85cb799c08d0c95ed8aee (patch)
treec953729c352e034a2f5f79e2759f756e17edb3c3 /ext/node_resolver/analyze.rs
parente53678fd5841fe7b94f8f9e16d6521201a3d2993 (diff)
perf(ext/node): reduce some allocations in require (#25197)
Diffstat (limited to 'ext/node_resolver/analyze.rs')
-rw-r--r--ext/node_resolver/analyze.rs12
1 files changed, 10 insertions, 2 deletions
diff --git a/ext/node_resolver/analyze.rs b/ext/node_resolver/analyze.rs
index 8d6a73424..8231cdc8a 100644
--- a/ext/node_resolver/analyze.rs
+++ b/ext/node_resolver/analyze.rs
@@ -1,5 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
+use std::borrow::Cow;
use std::collections::BTreeSet;
use std::collections::HashSet;
use std::path::Path;
@@ -583,9 +584,16 @@ fn not_found(path: &str, referrer: &Path) -> AnyError {
std::io::Error::new(std::io::ErrorKind::NotFound, msg).into()
}
-fn escape_for_double_quote_string(text: &str) -> String {
- text.replace('\\', "\\\\").replace('"', "\\\"")
+fn escape_for_double_quote_string(text: &str) -> Cow<str> {
+ // this should be rare, so doing a scan first before allocating is ok
+ if text.chars().any(|c| matches!(c, '"' | '\\')) {
+ // don't bother making this more complex for perf because it's rare
+ Cow::Owned(text.replace('\\', "\\\\").replace('"', "\\\""))
+ } else {
+ Cow::Borrowed(text)
+ }
}
+
#[cfg(test)]
mod tests {
use super::*;