Skip to content

Commit a4b05d2

Browse files
committed
fs: improve ExistsSync performance
1 parent d335487 commit a4b05d2

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

src/node_file.cc

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,24 +1003,14 @@ static void ExistsSync(const FunctionCallbackInfo<Value>& args) {
10031003
THROW_IF_INSUFFICIENT_PERMISSIONS(
10041004
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());
10051005

1006-
uv_fs_t req;
1007-
auto make = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); });
1006+
std::error_code error{};
10081007
FS_SYNC_TRACE_BEGIN(access);
1009-
int err = uv_fs_access(nullptr, &req, path.out(), 0, nullptr);
1008+
bool exists = std::filesystem::exists(path.ToStringView(), error);
10101009
FS_SYNC_TRACE_END(access);
1011-
1012-
#ifdef _WIN32
1013-
// In case of an invalid symlink, `uv_fs_access` on win32
1014-
// will **not** return an error and is therefore not enough.
1015-
// Double check with `uv_fs_stat()`.
1016-
if (err == 0) {
1017-
FS_SYNC_TRACE_BEGIN(stat);
1018-
err = uv_fs_stat(nullptr, &req, path.out(), nullptr);
1019-
FS_SYNC_TRACE_END(stat);
1010+
if (error) {
1011+
exists = false;
10201012
}
1021-
#endif // _WIN32
1022-
1023-
args.GetReturnValue().Set(err == 0);
1013+
args.GetReturnValue().Set(exists);
10241014
}
10251015

10261016
// Used to speed up module loading. Returns 0 if the path refers to

0 commit comments

Comments
 (0)