Skip to content

Commit

Permalink
fs: faster invalid failing for existsSync
Browse files Browse the repository at this point in the history
```
                                                          confidence improvement accuracy (*)   (**)  (***)
fs/bench-existsSync.js n=100000 type='existing'                         -1.29 %       ±1.97% ±2.61% ±3.40%
fs/bench-existsSync.js n=100000 type='invalid'                  ***     63.81 %       ±1.55% ±2.07% ±2.71%
fs/bench-existsSync.js n=100000 type='non-existing'             ***     -3.73 %       ±1.89% ±2.52% ±3.28%
fs/bench-existsSync.js n=100000 type='non-flat-existing'          *     -1.91 %       ±1.81% ±2.41% ±3.13%
```
  • Loading branch information
CanadaHonk committed Nov 26, 2023
1 parent f28839b commit 7a774cd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 5 additions & 2 deletions benchmark/fs/bench-existsSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const tmpfile = tmpdir.resolve(`.existing-file-${process.pid}`);
fs.writeFileSync(tmpfile, 'this-is-for-a-benchmark', 'utf8');

const bench = common.createBenchmark(main, {
type: ['existing', 'non-existing', 'non-flat-existing'],
n: [1e6],
type: ['existing', 'non-existing', 'non-flat-existing', 'invalid'],
n: [1e5],
});

function main({ n, type }) {
Expand All @@ -26,6 +26,9 @@ function main({ n, type }) {
case 'non-existing':
path = tmpdir.resolve(`.non-existing-file-${process.pid}`);
break;
case 'invalid':
path = -1;
break;
default:
new Error('Invalid type');
}
Expand Down
5 changes: 5 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,15 @@ ObjectDefineProperty(exists, kCustomPromisifiedSymbol, {
* @returns {boolean}
*/
function existsSync(path) {
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;

try {
path = getValidatedPath(path);
} catch {
return false;
} finally {
Error.stackTraceLimit = stackTraceLimit;
}

return binding.existsSync(pathModule.toNamespacedPath(path));
Expand Down

0 comments on commit 7a774cd

Please sign in to comment.