Skip to content

Commit 5c504b8

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

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/node_file.cc

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,24 +1003,23 @@ 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); });
1008-
FS_SYNC_TRACE_BEGIN(access);
1009-
int err = uv_fs_access(nullptr, &req, path.out(), 0, nullptr);
1010-
FS_SYNC_TRACE_END(access);
1006+
std::error_code error{};
1007+
auto file_path = std::filesystem::path(path.ToStringView());
1008+
bool exists = false;
10111009

10121010
#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);
1011+
FS_SYNC_TRACE_BEGIN(stat);
1012+
auto status = std::filesystem::status(file_path, error);
1013+
FS_SYNC_TRACE_END(stat);
1014+
if (!error) {
1015+
exists = status.type() != std::filesystem::file_type::not_found;
10201016
}
1021-
#endif // _WIN32
1022-
1023-
args.GetReturnValue().Set(err == 0);
1017+
#else
1018+
FS_SYNC_TRACE_BEGIN(access);
1019+
exists = std::filesystem::exists(file_path, error);
1020+
FS_SYNC_TRACE_END(access);
1021+
#endif
1022+
args.GetReturnValue().Set(exists);
10241023
}
10251024

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

0 commit comments

Comments
 (0)