Closed fasiha closed 4 years ago
Hmm. No I guess it returns them in the order they were added. Directories are stored as Map
s, and we just call map.keys()
, see:
My experience has been that Node's readdir
returns different ordering on different operating systems... so I always sort the results anyway when it matters.
A quick googling shows some examples of the craziness in Node / libuv:
So the short answer is: it doesn't return a sorted list because isomorphic-git always sorts the results of readdir so I never noticed or thought about it. In a kind of sick twist then, if we add sorting to LightningFS, that means every readdir list will end up being sorted twice, and that could actually make performance worse, so I kinda hate to do that. :-( That sucks.
I'll make a note in the README, because that's a definite "gotcha".
Makes sense, thank you!
I ran into unexpected behavior in an app because LightningFS'
readdir
can return non-sorted strings. The following example shows the issue in Node.js (make sure younpm install --save-dev fake-indexeddb
first), but the issue was first identified in the browser (Firefox and Safari):The script above writes to a set of fifteen files (whose names are between '0' and '14') in random order, and then prints the output of
readdir
. Running this script several times shows the issue: an unsorted list of files:This behavior persists whether I use numeric or alphanumeric filenames.
In contrast, if I use Node's
fs
module,readdir
returns sorted strings.Is this an IndexedDB limitation? If it is, a
sort
on eachreaddir
may be too expensive. But if not, maybe there's a workaround in the library itself?Loving the LightningFS + isomorphic-git lifestyle, thank you for your continued hard work on these projects!