Created
January 22, 2019 03:01
-
-
Save colus001/a889c563da0b90862a16e743df6708fe to your computer and use it in GitHub Desktop.
Measure Node.js File IO in terms of copy and write
This file contains 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 path = require('path'); | |
const rimraf = require('rimraf'); | |
const ProgressBar = require('progress'); | |
const readFrom = path.join(__dirname, './test.json'); | |
const file = fs.readFileSync(readFrom); | |
const getRandomNumber = (number = 1000000) => Math.floor(Math.random() * number); | |
const LENGTH = 10000; | |
const measure = (label, callback) => { | |
console.time(label); | |
var bar = new ProgressBar(' measuring ":label" [:bar] :percent :etas', { | |
complete: '=', | |
incomplete: ' ', | |
width: 40, | |
total: LENGTH | |
}); | |
Array.from({ length: LENGTH }).map(() => { | |
callback(); | |
bar.tick({ label }); | |
}) | |
console.timeEnd(label); | |
} | |
measure('write', () => { | |
const writeTo = path.join(__dirname, `./test.json.write.${getRandomNumber()}`); | |
fs.writeFileSync(writeTo, file, 'utf8'); | |
}); | |
measure('copy', () => { | |
const copyTo = path.join(__dirname, `./test.json.copy.${getRandomNumber()}`); | |
fs.copyFileSync(readFrom, copyTo); | |
}); | |
rimraf(path.join(__dirname, './test.json.write.*'), () => {}) | |
rimraf(path.join(__dirname, './test.json.copy.*'), () => {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test.json generated from json-generator