sync: 2591.664ms
async: 9187.473ms
callback: 9790.185ms
nextTick: 452.656ms
readStraightForward: 8755.447ms
sync: 151.101ms
async: 1480.068ms
callback: 1268.984ms
nextTick: 103.432ms
const fs = require('fs'); | |
const util = require('util'); | |
const readFile = util.promisify(fs.readFile); | |
fs.writeFileSync('a', 'a'); | |
const attempts = 100000; | |
function runInCallback(left, cb) { | |
fs.readFile('a', () => { | |
if (left > 0) { | |
runInCallback(left - 1, cb); | |
} else { | |
cb(); | |
} | |
}); | |
} | |
function readStraightForward(callback) { | |
// Implementation like in: https://github.com/nodejs/node/blob/v10.x/lib/fs.js | |
fs.open('a', 'r', 0o666, (err, fd) => { | |
if (err) { | |
callback(err); | |
return; | |
} | |
fs.fstat(fd, (err, stat) => { | |
if (err) { | |
callback(err); | |
return; | |
} | |
const buffer = Buffer.allocUnsafe(stat.size); | |
fs.read(fd, buffer, 0, stat.size, null, (err) => { | |
if (err) { | |
callback(err); | |
return; | |
} | |
fs.close(fd, () => { | |
callback(null, buffer); | |
}); | |
}); | |
}); | |
}); | |
} | |
const readStraightForwardPromisified = util.promisify(readStraightForward); | |
(async () => { | |
console.time('sync'); | |
for (let i = 0; i < attempts; i++) { | |
fs.readFileSync('a'); | |
} | |
console.timeEnd('sync'); | |
console.time('async'); | |
for (let i = 0; i < attempts; i++) { | |
await readFile('a'); | |
} | |
console.timeEnd('async'); | |
console.time('callback'); | |
await new Promise(r => runInCallback(attempts, r)); | |
console.timeEnd('callback'); | |
console.time('nextTick'); | |
for (let i = 0; i < attempts; i++) { | |
await new Promise(r => setImmediate(r)); | |
} | |
console.timeEnd('nextTick'); | |
console.time('readStraightForward'); | |
for (let i = 0; i < attempts; i++) { | |
await readStraightForwardPromisified(); | |
} | |
console.timeEnd('readStraightForward'); | |
})().catch(e => console.log(e)); |
With
--trace_gc