Skip to content

Instantly share code, notes, and snippets.

@kazuma0129
Last active July 29, 2022 16:41
Show Gist options
  • Save kazuma0129/7b8a350c8f377b49e2c7474a5755e66d to your computer and use it in GitHub Desktop.
Save kazuma0129/7b8a350c8f377b49e2c7474a5755e66d to your computer and use it in GitHub Desktop.
Collect files exact match using Map in Deno
const collectExactMatchFiles = async (p: string, cache: Map<string, string[]>) => {
const collect = async (key: string) => {
const c = await Deno.readTextFile(key);
if (!cache.has(c)) {
cache.set(c, [key]);
return;
}
const e = cache.get(c);
e && cache.set(c, [...e, key]);
};
const traverse = async (p: string) => {
const entries = Deno.readDir(p);
for await (const e of entries) {
const compPath = `${p}/${e.name}`;
if (e.isFile) {
await collect(compPath);
}
if (e.isDirectory) {
await traverse(compPath);
}
}
};
await traverse(p);
return cache;
};
const TARGET_DIR = Deno.args[0];
console.time('time');
const result = await collectExactMatchFiles(TARGET_DIR, new Map<string, string[]>());
console.log(result);
console.timeEnd('time');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment