From 34c9fc2e4e12fa9aa58d94a3283193444302a3ab Mon Sep 17 00:00:00 2001 From: Brian White Date: Fri, 13 Jan 2017 05:07:18 -0500 Subject: [PATCH] fs: avoid multiple conversions to string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit nullCheck() implicitly converts the argument to string when checking the value, so this commit avoids any unnecessary additional (Buffer) conversions to string. PR-URL: https://github.com/nodejs/node/pull/10789 Reviewed-By: James M Snell Reviewed-By: Michaƫl Zasso Reviewed-By: Michael Dawson Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum --- lib/fs.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index eab8d942e183f0..3e9a0540dd9d12 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1503,21 +1503,21 @@ function encodeRealpathResult(result, options) { fs.realpathSync = function realpathSync(p, options) { options = getOptions(options, {}); handleError((p = getPathFromURL(p))); + if (typeof p !== 'string') + p += ''; nullCheck(p); - - p = p.toString('utf8'); p = pathModule.resolve(p); - const seenLinks = {}; - const knownHard = {}; const cache = options[internalFS.realpathCacheKey]; - const original = p; - const maybeCachedResult = cache && cache.get(p); if (maybeCachedResult) { return maybeCachedResult; } + const seenLinks = {}; + const knownHard = {}; + const original = p; + // current character position in p var pos; // the partial path so far, including a trailing slash if any @@ -1614,10 +1614,10 @@ fs.realpath = function realpath(p, options, callback) { options = getOptions(options, {}); if (handleError((p = getPathFromURL(p)), callback)) return; + if (typeof p !== 'string') + p += ''; if (!nullCheck(p, callback)) return; - - p = p.toString('utf8'); p = pathModule.resolve(p); const seenLinks = {};