Skip to content

existsSync is expensive #24008

Closed
Closed
@arcanis

Description

@arcanis
  • Version: master
  • Platform: n/a
  • Subsystem: fs

The current implementation of fs.existsSync involves wrapping accessSync in a try/catch statement. While conceptually simple, it has one fundamental flaw: it means that Node has to instance an Error for each call to fs.existsSync made on non-existing paths. This can end up very expensive.

Would it be possible to make this function more lightweight by simply checking the return value of the libuv's access call? I'm not familiar with the Node internals, but I feel like simply returning false instead of calling handleErrorFromBinding here would be sufficient.

Relevant lines:

node/lib/fs.js

Lines 229 to 236 in e35f671

function existsSync(path) {
try {
fs.accessSync(path, F_OK);
return true;
} catch (e) {
return false;
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions