Skip to content

Commit 34c9fc2

Browse files
committed
fs: avoid multiple conversions to string
nullCheck() implicitly converts the argument to string when checking the value, so this commit avoids any unnecessary additional (Buffer) conversions to string. PR-URL: #10789 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 21b2440 commit 34c9fc2

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

lib/fs.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1503,21 +1503,21 @@ function encodeRealpathResult(result, options) {
15031503
fs.realpathSync = function realpathSync(p, options) {
15041504
options = getOptions(options, {});
15051505
handleError((p = getPathFromURL(p)));
1506+
if (typeof p !== 'string')
1507+
p += '';
15061508
nullCheck(p);
1507-
1508-
p = p.toString('utf8');
15091509
p = pathModule.resolve(p);
15101510

1511-
const seenLinks = {};
1512-
const knownHard = {};
15131511
const cache = options[internalFS.realpathCacheKey];
1514-
const original = p;
1515-
15161512
const maybeCachedResult = cache && cache.get(p);
15171513
if (maybeCachedResult) {
15181514
return maybeCachedResult;
15191515
}
15201516

1517+
const seenLinks = {};
1518+
const knownHard = {};
1519+
const original = p;
1520+
15211521
// current character position in p
15221522
var pos;
15231523
// the partial path so far, including a trailing slash if any
@@ -1614,10 +1614,10 @@ fs.realpath = function realpath(p, options, callback) {
16141614
options = getOptions(options, {});
16151615
if (handleError((p = getPathFromURL(p)), callback))
16161616
return;
1617+
if (typeof p !== 'string')
1618+
p += '';
16171619
if (!nullCheck(p, callback))
16181620
return;
1619-
1620-
p = p.toString('utf8');
16211621
p = pathModule.resolve(p);
16221622

16231623
const seenLinks = {};

0 commit comments

Comments
 (0)