const fs = require('fs').promises let cachedContents function getContents() { // If we have already read the file, just return its contents if (cachedContents !== undefined) { return Promise.resolve(cachedContents) } // Otherwise, read the file and store its contents const readPromise = fs.readFile('file.txt', 'utf8') readPromise.then(contents => { cachedContents = contents }) return readPromise } const start1 = process.hrtime() getContents().then(_ => { // Reading file took 1946343 ns const time1 = process.hrtime(start1) console.log(`Reading file took ${time1[1]} ns`) const start2 = process.hrtime() getContents().then(_ => { // Using cached file took 29273 ns const time2 = process.hrtime(start2) console.log(`Using cached file took ${time2[1]} ns`) }) })