diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2024-07-17 05:35:51 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-17 18:05:51 +0530 |
commit | 078def0ff8501bb07f3f286515acd8c6a2181037 (patch) | |
tree | 6811ec33761ede11e5213393e7ea3732fd54f292 /ext/node/polyfills/_fs | |
parent | 568dd132fb0a47f9afb11bffec341c7481dda75c (diff) |
perf(ext/node): optimize fs.exists[Sync] (#24613)
Use `access` on *nix and `GetFileAttributesW` on Windows.
[Benchmark](https://paste.divy.work/p/-gq8Ark.js):
```
$ deno run -A bench.mjs # main (568dd)
existsSync: 8980.636629ms
$ target/release/deno run -A bench.mjs # this PR
existsSync: 6448.7604519999995ms
$ bun bench.mjs
existsSync: 6562.88671ms
$ node bench.mjs
existsSync: 7740.064653ms
```
Ref https://github.com/denoland/deno/pull/24434#discussion_r1679777912
Diffstat (limited to 'ext/node/polyfills/_fs')
-rw-r--r-- | ext/node/polyfills/_fs/_fs_exists.ts | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/ext/node/polyfills/_fs/_fs_exists.ts b/ext/node/polyfills/_fs/_fs_exists.ts index 57df1f07c..b5bbe235a 100644 --- a/ext/node/polyfills/_fs/_fs_exists.ts +++ b/ext/node/polyfills/_fs/_fs_exists.ts @@ -3,7 +3,7 @@ // TODO(petamoriken): enable prefer-primordials for node polyfills // deno-lint-ignore-file prefer-primordials -import { op_node_fs_exists_sync } from "ext:core/ops"; +import { op_node_fs_exists, op_node_fs_exists_sync } from "ext:core/ops"; import { pathFromURL } from "ext:deno_web/00_infra.js"; @@ -16,7 +16,7 @@ type ExistsCallback = (exists: boolean) => void; */ export function exists(path: string | URL, callback: ExistsCallback) { path = path instanceof URL ? pathFromURL(path) : path; - Deno.lstat(path).then(() => callback(true), () => callback(false)); + op_node_fs_exists(path).then(callback); } // The callback of fs.exists doesn't have standard callback signature. |