mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
* Implement `fs.readdir(path, {recursive: true})` and `fs.readdirSync(path, {recursive: true})`
* Update node_fs.zig
* FIx memory leak in error code
* Add fail test
* Update readdir.mjs
* Update bun.zig
* Update readdir.mjs
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
51 lines
1.7 KiB
JavaScript
51 lines
1.7 KiB
JavaScript
import { readdirSync, readdir as readdirCb } from "fs";
|
|
import { readdir } from "fs/promises";
|
|
import { bench, run } from "./runner.mjs";
|
|
import { argv } from "process";
|
|
import { fileURLToPath } from "url";
|
|
import { relative, resolve } from "path";
|
|
import { createHash } from "crypto";
|
|
|
|
let dir = resolve(argv.length > 2 ? argv[2] : fileURLToPath(new URL("../../node_modules", import.meta.url)));
|
|
if (dir.includes(process.cwd())) {
|
|
dir = relative(process.cwd(), dir);
|
|
}
|
|
|
|
const result = await readdir(dir, { recursive: true });
|
|
const count = result.length;
|
|
const syncCount = readdirSync(dir, { recursive: true }).length;
|
|
|
|
const hash = createHash("sha256").update(result.sort().join("\n")).digest("hex");
|
|
|
|
bench(`await readdir("${dir}", {recursive: true})`, async () => {
|
|
await readdir(dir, { recursive: true });
|
|
});
|
|
|
|
bench(`await readdir("${dir}", {recursive: true}) x 10`, async () => {
|
|
const promises = [
|
|
readdir(dir, { recursive: true }),
|
|
readdir(dir, { recursive: true }),
|
|
readdir(dir, { recursive: true }),
|
|
readdir(dir, { recursive: true }),
|
|
readdir(dir, { recursive: true }),
|
|
readdir(dir, { recursive: true }),
|
|
readdir(dir, { recursive: true }),
|
|
readdir(dir, { recursive: true }),
|
|
readdir(dir, { recursive: true }),
|
|
readdir(dir, { recursive: true }),
|
|
readdir(dir, { recursive: true }),
|
|
];
|
|
await Promise.all(promises);
|
|
});
|
|
|
|
bench(`await readdir("${dir}", {recursive: false})`, async () => {
|
|
await readdir(dir, { recursive: false });
|
|
});
|
|
|
|
await run();
|
|
console.log("\n", count, "files/dirs in", dir, "\n", "SHA256:", hash, "\n");
|
|
|
|
if (count !== syncCount) {
|
|
throw new Error(`Mismatched file counts: ${count} async !== ${syncCount} sync`);
|
|
}
|