Why is asynchronous file reading slower than synchronous file reading?

Viewed 131

I want to read many files (about 30 files), their paths are contained in a string array. I thought if I ran many reading operations at once it will be much faster than sync:

Sync

Async

But benchmark shows unexpected results:

Sync:

  console.time('Sync')
  files.forEach(fileName => fs.readFileSync(fileName))
  console.timeEnd('Sync')

Sync: 0.205ms

Sync: 0.198ms

Sync: 0.214ms

Async:

  console.time('Async')
  const promises = files.map(fileName => fs.readFile(fileName))
  await Promise.all(promises)
  console.timeEnd('Async')

Async: 1.155ms

Async: 1.05ms

Async: 1.236ms

What's going on?

0 Answers
Related