From f969fe68b34a124268a63f3f4d5a1075efb0aa4a Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 21 Sep 2026 04:27:43 +0000 Subject: [PATCH] path: skip normalize for already-normalized paths If a posix path contains no '.' and no '//', it has no '.'/'..' segments and no empty segments, so the result is the input. Official benchmark/path/normalize-posix.js: /foo/bar is about 66% faster and /foo is about 31% faster. Paths that still need normalizeString are slightly slower because of the two indexOf probes. Assisted-by: a closed-source coding agent Signed-off-by: Yagiz Nizipli Co-authored-by: Yagiz Nizipli --- lib/path.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/path.js b/lib/path.js index 658b5d6901df..470ae54b3fca 100644 --- a/lib/path.js +++ b/lib/path.js @@ -1295,6 +1295,12 @@ const posix = { if (path.length === 0) return '.'; + // Already normalized: no '.' / '..' segments and no empty '//' segments. + if (StringPrototypeIndexOf(path, '.') === -1 && + StringPrototypeIndexOf(path, '//') === -1) { + return path; + } + const isAbsolute = path[0] === '/'; const trailingSeparator = path[path.length - 1] === '/';