I am using Typescript and therefore libuv to perform any IO operations. In my specific scenario I am taking a fingerprint hash of a given file. For the sake of making a point here to my question, consider the input file being a file of 1TB. To get a fingerprint of the file, I could open the file through a file stream and update the hash:
return new Promise((resolve, reject) => {
const hash = crypto.createHash('sha256');
const fh = fse.createReadStream(filepath, {
highWaterMark : 100000000
});
fh.on('data', (d) => { hash.update(d); });
fh.on('end', () => {
resolve(hash);
});
fh.on('error', reject);
});
The example above is fairly slow given it's sequential approach. So a faster approach I was thinking about is to split the calculation up into N blocks like this:
let promises = [];
for (let i = 0; i < N; ++i) {
promises.push(calculateFilePart(file, from, to));
}
return Promise.all(all);
Given the example above, imagine N is 1000000, does that mean libuv starts 1000000 asynchronous I/O operations in the background at the same time? Or does libuv queue them automatically in batches to avoid an oversaturation of IO requests?
Any help in this topic is highly appreciated!