Offload functions to worker threads with shared memory primitives for Node.js.
1// Walk a directory tree with a recursive async generator and hash every file
2// using map() with bounded concurrency. Results stream back in completion order.
3// Requires Node v24+.
4//
5// Run: node examples/bounded-map/main.ts [dir]
6
7import { readdir } from 'node:fs/promises';
8import { join } from 'node:path';
9import { workers, map, inert } from '../../src/index.ts';
10import type { Task } from '../../src/index.ts';
11import { hashFile, type FileHash } from './hash-file.ts';
12
13const root = process.argv[2] ?? './src';
14
15{
16 using run = workers();
17 const start = performance.now();
18 let count = 0;
19 for await (const { path, hash } of map(run, walk(root), { concurrency: 4 })) {
20 console.log(`${hash.slice(0, 12)} ${path}`);
21 count++;
22 }
23 console.log(`\nhashed ${count} files in ${(performance.now() - start).toFixed(0)}ms`);
24}
25
26async function* walk(dir: string): AsyncGenerator<Task<FileHash>> {
27 for (const entry of await readdir(dir, { withFileTypes: true })) {
28 const p = join(dir, entry.name);
29 if (entry.isDirectory()) {
30 yield* walk(p);
31 } else {
32 yield inert(hashFile(p));
33 }
34 }
35}