Skip to content

Commit

Permalink
perf: improvements using Promise.all() (#3683)
Browse files Browse the repository at this point in the history
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
  • Loading branch information
sanjaiyan-dev and iuioiua authored Oct 12, 2023
1 parent b8cd759 commit 5b73e8b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
10 changes: 7 additions & 3 deletions fs/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,21 @@ async function copyDir(
src = toPathString(src);
dest = toPathString(dest);

const promises = [];

for await (const entry of Deno.readDir(src)) {
const srcPath = join(src, entry.name);
const destPath = join(dest, basename(srcPath as string));
if (entry.isSymlink) {
await copySymLink(srcPath, destPath, options);
promises.push(copySymLink(srcPath, destPath, options));
} else if (entry.isDirectory) {
await copyDir(srcPath, destPath, options);
promises.push(copyDir(srcPath, destPath, options));
} else if (entry.isFile) {
await copyFile(srcPath, destPath, options);
promises.push(copyFile(srcPath, destPath, options));
}
}

await Promise.all(promises);
}

/* copy folder from src to dest synchronously */
Expand Down
7 changes: 3 additions & 4 deletions fs/empty_dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ export async function emptyDir(dir: string | URL) {
items.push(dirEntry);
}

while (items.length) {
const item = items.shift();
await Promise.all(items.map((item) => {
if (item && item.name) {
const filepath = join(toPathString(dir), item.name);
await Deno.remove(filepath, { recursive: true });
return Deno.remove(filepath, { recursive: true });
}
}
}));
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
Expand Down
4 changes: 3 additions & 1 deletion http/cookie_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,11 @@ export class SecureCookieMap extends CookieMapBase {
/** Sets all cookies in the {@linkcode Request} to be deleted in the
* response. */
async clear(options: SecureCookieMapSetDeleteOptions) {
const promises = [];
for await (const key of this.keys()) {
await this.set(key, null, options);
promises.push(this.set(key, null, options));
}
await Promise.all(promises);
}

/** Set a cookie to be deleted in the response.
Expand Down

0 comments on commit 5b73e8b

Please sign in to comment.