Skip to content

Instantly share code, notes, and snippets.

@ekoneko
Last active August 27, 2017 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekoneko/2909d8f91b095c35217aac34857df994 to your computer and use it in GitHub Desktop.
Save ekoneko/2909d8f91b095c35217aac34857df994 to your computer and use it in GitHub Desktop.
node file sync test
const fs = require('fs');
const startTime = Date.now();
console.log('start at', startTime);
setTimeout(() => {
const endTime = Date.now();
console.log('0.01 sec later', endTime);
console.log(endTime - startTime)
}, 10)
const filePath = '/Users/kaze/Code/js/归档.zip'
if (process.argv[2] === 'sync') {
const readFileStart = Date.now();
console.log('read file start', readFileStart);
fs.readFileSync(filePath)
const readFileEnd = Date.now();
console.log('read file end', readFileEnd);
console.log('read file during', readFileEnd - readFileStart);
} else {
fs.readFile(filePath, (err, file) => {})
}
@ekoneko
Copy link
Author

ekoneko commented Aug 27, 2017

/Users/kaze/Code/js/归档.zip is a file cost 100ms for i/o reading

The response is

node test.js  sync
start at 1503807147461
read file start 1503807147480
read file end 1503807147774
read file during 294
0.01 sec later 1503807147775
314
node test.js      
start at 1503807150126
0.01 sec later 1503807150154
28

So, readFileSync will block other async tasks response time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment