diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2019-04-14 17:34:08 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2019-04-26 18:43:11 +0200 |
commit | 1d022e825356dfba9bef2d6f194b9e93d63b726d (patch) | |
tree | b102789e5cdf7c399596b96e3818cbff96f62e89 /lib/internal/process | |
parent | 55147d7d9939cce1bbc2c8a70ac7ebe03e91e5cf (diff) | |
download | android-node-v8-1d022e825356dfba9bef2d6f194b9e93d63b726d.tar.gz android-node-v8-1d022e825356dfba9bef2d6f194b9e93d63b726d.tar.bz2 android-node-v8-1d022e825356dfba9bef2d6f194b9e93d63b726d.zip |
process: improve cwd performance
This caches the current working directory and only updates the variable
if `process.chdir()` is called.
PR-URL: https://github.com/nodejs/node/pull/27224
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Diffstat (limited to 'lib/internal/process')
-rw-r--r-- | lib/internal/process/main_thread_only.js | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/lib/internal/process/main_thread_only.js b/lib/internal/process/main_thread_only.js index 358e2b37b0..0cb3edbf9a 100644 --- a/lib/internal/process/main_thread_only.js +++ b/lib/internal/process/main_thread_only.js @@ -20,9 +20,15 @@ const { signals } = internalBinding('constants').os; // The execution of this function itself should not cause any side effects. function wrapProcessMethods(binding) { + // Cache the working directory to prevent lots of lookups. If the working + // directory is changed by `chdir`, it'll be updated. + let cachedCwd = ''; + function chdir(directory) { validateString(directory, 'directory'); - return binding.chdir(directory); + binding.chdir(directory); + // Mark cache that it requires an update. + cachedCwd = ''; } function umask(mask) { @@ -32,9 +38,16 @@ function wrapProcessMethods(binding) { return binding.umask(mask); } + function cwd() { + if (cachedCwd === '') + cachedCwd = binding.cwd(); + return cachedCwd; + } + return { chdir, - umask + umask, + cwd }; } |