Last active
August 27, 2017 04:19
-
-
Save ekoneko/2909d8f91b095c35217aac34857df994 to your computer and use it in GitHub Desktop.
node file sync test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => {}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/Users/kaze/Code/js/归档.zip
is a file cost 100ms for i/o readingThe response is
So,
readFileSync
will block other async tasks response time