How to read files quickly in JavaScript

How to read files quickly in JavaScript

Daniel Lemire's blog

Suppose you need to read several files on a server using JavaScript. There are many ways to read files in JavaScript with a runtime like Node.js. Which one is best? Let us consider the various approaches.

Using fs.promises

const fs = require('fs/promises');
const readFile = fs.readFile;
readFile("lipsum.txt", { encoding: 'utf-8' })
.then((data) => {...})
.catch((err) => {...})

Using fs.readFile and util.promisify

const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
readFile("lipsum.txt", { encoding: 'utf-8' })
.then((data) => {...})
.catch((err) => {...})

Using fs.readFileSync

const fs = require('fs');
const readFileSync = fs.readFileSync;
var data = readFileSync("lipsum.txt", { encoding: 'utf-8' })

Using await fs.readFileSync

const fs = require('fs');
const readFileSync = fs.readFileSync;
async function f(name, options) {
  return await readFileSync(name, options);
}

Using fs.readFile

const fs = require('fs');
const readFile = fs.readFile;
fs.readFile('lipsum.txt', function read(err, data) {...});

Benchmark

I wrote a small benchmark where I repeated read a file from disk. It is a simple loop where the same file is accessed each time. I report the number of milliseconds needed to read the file 50,000 times.  The file is relatively small (slightly over a kilobyte). I use a large server with dozens of Ice Lake Intel cores and plenty of memory. I use Node.js 20.1 and Bun 1.0.14.

I ran the benchmarks multiple times, and I report the best results in all cases. Your results will differ.

At least on my system, in this test, the fs.promises is significantly more expensive than anything else when using Node.js. The Bun runtime is much faster than Node.js in this test.

Credit. My benchmark is inspired by a test case provided by Evgenii Stulnikov.

Generated by RSStT. The copyright belongs to the original author.

Source

Report Page