From 26607a650e7a99b2e1f021c8e0864be4e8b205b4 Mon Sep 17 00:00:00 2001 From: Lee Jiho Date: Tue, 9 Sep 2025 03:44:18 +0900 Subject: [PATCH] path: refactor path joining logic for clarity and performance PR-URL: https://github.com/nodejs/node/pull/59781 Reviewed-By: James M Snell Reviewed-By: Minwoo Jung Reviewed-By: Luigi Pinca --- lib/path.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/path.js b/lib/path.js index be6fd8b24a..63b037cddf 100644 --- a/lib/path.js +++ b/lib/path.js @@ -24,6 +24,7 @@ const { ArrayPrototypeIncludes, ArrayPrototypeJoin, + ArrayPrototypePush, ArrayPrototypeSlice, FunctionPrototypeBind, StringPrototypeCharCodeAt, @@ -506,22 +507,21 @@ const win32 = { if (args.length === 0) return '.'; - let joined; - let firstPart; + const path = []; for (let i = 0; i < args.length; ++i) { const arg = args[i]; validateString(arg, 'path'); if (arg.length > 0) { - if (joined === undefined) - joined = firstPart = arg; - else - joined += `\\${arg}`; + ArrayPrototypePush(path, arg); } } - if (joined === undefined) + if (path.length === 0) return '.'; + const firstPart = path[0]; + let joined = ArrayPrototypeJoin(path, '\\'); + // Make sure that the joined path doesn't start with two slashes, because // normalize() will mistake it for a UNC path then. //