Skip to content

Commit 9dd2903

Browse files
authored
perf: improve call to Path::ends_with (#199)
`Path::ends_with` converts the comparator to a `Path` and compares all components. `module_name` in `cached_path.path().ends_with(module_name)` is guaranteed to be a single component so this overhead can be avoided. ```rust #[must_use] pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool { self._ends_with(child.as_ref()) } fn _ends_with(&self, child: &Path) -> bool { iter_after(self.components().rev(), child.components().rev()).is_some() } ```
1 parent 94efa90 commit 9dd2903

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,10 +742,12 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
742742
module_name: &str,
743743
ctx: &mut Ctx,
744744
) -> Option<CachedPath> {
745-
if cached_path.path().ends_with(module_name) {
746-
Some(cached_path.clone())
747-
} else if module_name == "node_modules" {
745+
if module_name == "node_modules" {
748746
cached_path.cached_node_modules(&self.cache, ctx)
747+
} else if cached_path.path().components().next_back()
748+
== Some(Component::Normal(OsStr::new(module_name)))
749+
{
750+
Some(cached_path.clone())
749751
} else {
750752
cached_path.module_directory(module_name, &self.cache, ctx)
751753
}

0 commit comments

Comments
 (0)