Created
February 12, 2024 20:29
-
-
Save fearthecowboy/62f4f0432a7b01c28048bebf6818149d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S deno run --reload -A --ext=ts | |
` <#`; | |
import { readdir } from "node:fs/promises"; | |
import { join,resolve } from "node:path"; | |
import { argv } from "node:process"; | |
let size = 0; | |
let count = 0; | |
function fmt(n:number) { | |
// return a string with commas | |
n = Math.trunc(n); | |
return n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); | |
} | |
function fmt2(n: number) { | |
// return a string with commas | |
n = Math.trunc(n*100)/100; | |
return n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); | |
} | |
function sz(n:number) { | |
if( n < 1024 ) { | |
return `${fmt(n)} bytes`; | |
} | |
n = n / 1024; | |
if( n < 1024 ) { | |
return `${fmt2(n)} kb`; | |
} | |
n = n / 1024; | |
if( n < 1024 ) { | |
return `${fmt2(n)} mb`; | |
} | |
n = n / 1024; | |
return `${(fmt2(n))} gb`; | |
} | |
async function readFile(filePath: string) { | |
await Deno.readTextFile(filePath).catch(() => { }); | |
count++; | |
if (count % 1000 === 0) { | |
console.log(`Total Scanned: ${fmt(count)} for ${sz(size)}`); | |
} | |
} | |
async function readFilesRecursively(dirPath: string) { | |
// Read the contents of the directory | |
const files = await readdir(dirPath); | |
const all = files.map(async file => { | |
const filePath = join(dirPath, file); | |
const stats = await Deno.stat(filePath); | |
if (stats.isDirectory) { | |
return readFilesRecursively(filePath); | |
} else { | |
// Print the file path for demonstration purposes | |
if (stats.isFile) { | |
size += stats.size; | |
return readFile(filePath); | |
} | |
} | |
}); | |
await Promise.all(all); | |
} | |
// Provide the path to the starting directory | |
const startingDirectory = resolve( argv.length > 2 ? argv[2] : '.'); | |
console.log(`Testing read time in folder: ${startingDirectory}`); | |
const start = performance.now(); | |
// Call the function to read files recursively | |
await readFilesRecursively(startingDirectory).catch(console.error); | |
const duration = performance.now() - start; | |
console.log(`Results: ${fmt(count)} files - ${sz(size)} in ${fmt(duration)} ms - ${sz((size) / (duration / 1000))} /second`); | |
/*#> if (-not (get-command deno)) { irm https://deno.land/install.ps1 | iex } ; deno run --reload -A --ext=ts ($MyInvocation.MyCommand.Path) @args #*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment