Skip to content

Instantly share code, notes, and snippets.

@nojaf
Created September 11, 2025 07:34
Show Gist options
  • Save nojaf/f3c26f2354b1609366bb51c59bfed20e to your computer and use it in GitHub Desktop.
Save nojaf/f3c26f2354b1609366bb51c59bfed20e to your computer and use it in GitHub Desktop.
import { readdir, stat as statAsync } from "fs/promises";
import { join } from "path";
// benchmark.js
const start = process.hrtime.bigint();
// --- your code goes here ---
const args = process.argv.slice(2);
if (args.length === 0) {
console.log(`
Usage: node find-runtime.mjs <project-folder>
Find @rescript/runtime directories in a project's node_modules.
Arguments:
project-folder Path to the project directory to search
Examples:
node find-runtime.mjs /path/to/project
node find-runtime.mjs .
`);
process.exit(1);
}
const project = args[args.length - 1];
// Efficient parallel folder traversal to find node_modules directories
async function findNodeModulesDirs(rootPath, maxDepth = 12) {
const nodeModulesDirs = [];
const visited = new Set();
async function traverseDir(currentPath, depth = 0) {
if (depth > maxDepth || visited.has(currentPath)) {
return;
}
visited.add(currentPath);
try {
const entries = await readdir(currentPath);
// Check if current directory has node_modules
if (entries.includes("node_modules")) {
const nodeModulesPath = join(currentPath, "node_modules");
try {
const stat = await statAsync(nodeModulesPath);
if (stat.isDirectory()) {
nodeModulesDirs.push(nodeModulesPath);
return; // Stop traversing this branch once we find node_modules
}
} catch (e) {
// node_modules exists but isn't a directory, continue
}
}
// Continue traversing subdirectories in parallel
const subdirPromises = entries
.filter((entry) => !entry.startsWith(".") && entry !== "node_modules")
.map(async (entry) => {
const fullPath = join(currentPath, entry);
try {
const stat = await statAsync(fullPath);
if (stat.isDirectory()) {
return traverseDir(fullPath, depth + 1);
}
} catch (e) {
// Skip inaccessible directories
}
});
await Promise.all(subdirPromises);
} catch (e) {
// Skip inaccessible directories
}
}
await traverseDir(rootPath);
return nodeModulesDirs;
}
// Custom function to find Deno vendorized @rescript/runtime directories
async function findDenoRescriptRuntime(nodeModulesPath) {
const results = [];
// First, check if there's a .deno directory directly in node_modules
const denoPath = join(nodeModulesPath, ".deno");
try {
const denoStat = await statAsync(denoPath);
if (denoStat.isDirectory()) {
await searchRescriptRuntimeInDeno(denoPath);
}
} catch (e) {
// .deno doesn't exist in node_modules, continue
}
// If no .deno found in node_modules, search in subdirectories (but limit depth)
if (results.length === 0) {
await searchDenoDir(nodeModulesPath, 0, 2); // Limit to 2 levels deep for faster search
}
async function searchDenoDir(currentPath, depth, maxDepth) {
if (depth >= maxDepth) return;
try {
const entries = await readdir(currentPath);
// Look for .deno directory
if (entries.includes(".deno")) {
const denoPath = join(currentPath, ".deno");
try {
const denoStat = await statAsync(denoPath);
if (denoStat.isDirectory()) {
await searchRescriptRuntimeInDeno(denoPath);
}
} catch (e) {
// Skip if not accessible
}
}
// Continue searching in subdirectories (but only if we haven't found anything yet)
if (results.length === 0) {
const subdirPromises = entries
.filter((entry) => !entry.startsWith(".") && entry !== "node_modules")
.map(async (entry) => {
const fullPath = join(currentPath, entry);
try {
const stat = await statAsync(fullPath);
if (stat.isDirectory()) {
return searchDenoDir(fullPath, depth + 1, maxDepth);
}
} catch (e) {
// Skip inaccessible directories
}
});
await Promise.all(subdirPromises);
}
} catch (e) {
// Skip inaccessible directories
}
}
async function searchRescriptRuntimeInDeno(denoPath) {
try {
const entries = await readdir(denoPath);
// Look for @rescript+runtime@* directories
const rescriptRuntimeDirs = entries.filter((entry) =>
entry.startsWith("@rescript+runtime@"),
);
for (const dir of rescriptRuntimeDirs) {
const fullPath = join(
denoPath,
dir,
"node_modules",
"@rescript",
"runtime",
);
try {
const stat = await statAsync(fullPath);
if (stat.isDirectory()) {
results.push(fullPath);
}
} catch (e) {
// Directory doesn't exist, continue
}
}
} catch (e) {
// Skip if not accessible
}
}
return results;
}
// Find all node_modules directories using efficient traversal
const node_modules = await findNodeModulesDirs(project);
console.log(node_modules);
const rescriptRuntimeDirs = await Promise.all(
node_modules.map(async (nm) => {
const results = [];
// Check for standard layout: @rescript/runtime
const standardPath = join(nm, "@rescript", "runtime");
try {
const stat = await statAsync(standardPath);
if (stat.isDirectory()) {
results.push(standardPath);
// If we found standard layout, no need to search for Deno layouts
return results;
}
} catch (e) {
// Directory doesn't exist, continue
}
// Only check for Deno vendorized layouts if standard layout wasn't found
const denoResults = await findDenoRescriptRuntime(nm);
results.push(...denoResults);
return results;
}),
).then((results) => results.flatMap((x) => x));
console.log(rescriptRuntimeDirs);
// ----------------------------
const end = process.hrtime.bigint();
const durationMs = Number(end - start) / 1e6; // convert ns → ms
console.log(`Script took ${durationMs.toFixed(3)}ms`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment