tmp
Thursday, November 1, 2018
fs performance (sync vs async)
```js const fs = require('fs') const path = require('path') const util = require('util') const calcSync = (filepath) => { const stats = fs.lstatSync(filepath) if (!stats.isDirectory()) { return [1, stats.size] } let totalNum = 0 let totalSize = 0 const filenames = fs.readdirSync(filepath) for (let filename of filenames) { const [num, size] = calcSync(path.join(filepath, filename)) totalNum += num totalSize += size } return [totalNum, totalSize] } const calc = async (filepath) => { const stats = await util.promisify(fs.lstat)(filepath) if (!stats.isDirectory()) { return [1, stats.size] } let totalNum = 0 let totalSize = 0 const filenames = await util.promisify(fs.readdir)(filepath) for (let filename of filenames) { const [num, size] = await calc(path.join(filepath, filename)) totalNum += num totalSize += size } return [totalNum, totalSize] } const src = '
' ;(async () => { console.time('calcSync') console.log(calcSync(src)) console.timeEnd('calcSync') console.time('calc') const r = await calc(src) console.log(r) console.timeEnd('calc') })() ``` ``` [ 23723, 359622209 ] calcSync: 352.919ms [ 23723, 359622209 ] calc: 1406.648ms ```
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment