thecodrr / fdir

⚡ The fastest directory crawler & globbing library for NodeJS. Crawls 1m files in < 1s
https://thecodrr.github.io/fdir/
MIT License
1.51k stars 58 forks source link

Remove callback API in next major #69

Closed vjpr closed 3 years ago

vjpr commented 3 years ago

Code could be simplified by removing the callback API. There are native promise apis for most functions now.

Not sure how performance is effected though - this would be the primary concern.

import { readdir } from 'fs/promises';

try {
  const files = await readdir(path);
  for (const file of files)
    console.log(file);
} catch (err) {
  console.error(err);
}

Would pave the way for supporting modern API interfaces such as Node Streams, Web Streams, AsyncInterators. AbortController could also be used for early-exit scenarios.

thecodrr commented 3 years ago

Promise API is 3 to 4 times slower. I have tested this extensively. Callback API fits the JS "parallelism" very well.

Would pave the way for supporting modern API interfaces such as Node Streams, Web Streams, AsyncInterators. AbortController could also be used for early-exit scenarios.

Most of these things are unnecessary. Async Iterators make sense but streaming is pointless; not to mention that it is trivial to convert iterators to Streams. AbortController would add unnecessary overhead of checking on every iteration if cancellation is requested — something you can easily do with an Iterator and a for-loop.