From 509039fcafbadbc0ea89f8d7d9985d308eb312ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Sun, 23 Jul 2017 21:48:33 +0200 Subject: [PATCH] path: remove unnecessary string copies As the length of `path` is known at this point, there is no point in making an exact copy using `slice`. PR-URL: https://github.com/nodejs/node/pull/14438 Reviewed-By: Refael Ackermann Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Luigi Pinca Reviewed-By: Timothy Gu Reviewed-By: Colin Ihrig Reviewed-By: Khaidi Chu --- lib/path.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/path.js b/lib/path.js index 2f67c8cadc8bd1..4dc4b8398464d3 100644 --- a/lib/path.js +++ b/lib/path.js @@ -788,7 +788,9 @@ const win32 = { } } } else if (code === 47/*/*/ || code === 92/*\*/) { - return path[0]; + // `path` contains just a path separator, exit early to avoid + // unnecessary work + return path; } for (var i = len - 1; i >= offset; --i) { @@ -1041,7 +1043,7 @@ const win32 = { if (len === 3) { // `path` contains just a drive root, exit early to avoid // unnecessary work - ret.root = ret.dir = path.slice(0, 3); + ret.root = ret.dir = path; return ret; } isAbsolute = true; @@ -1050,7 +1052,7 @@ const win32 = { } else { // `path` contains just a drive root, exit early to avoid // unnecessary work - ret.root = ret.dir = path.slice(0, 2); + ret.root = ret.dir = path; return ret; } } @@ -1058,7 +1060,7 @@ const win32 = { } else if (code === 47/*/*/ || code === 92/*\*/) { // `path` contains just a path separator, exit early to avoid // unnecessary work - ret.root = ret.dir = path[0]; + ret.root = ret.dir = path; return ret; }