Created
March 6, 2021 01:44
-
-
Save piglovesyou/29c15f0d386f4343a5f956d1928044ae to your computer and use it in GitHub Desktop.
Speed comparison of Node 15 file access with factors size and methods (sync, async and p-map concurrency)
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
import { readFileSync, writeFileSync, unlinkSync, promises } from "fs"; | |
import pMap from "p-map"; | |
const { readFile, writeFile, unlink } = promises; | |
const testCount = 10_000; | |
main().catch(err => (console.error(err), process.exit(1))); | |
async function main() { | |
const sizeBase = 1024; | |
for (let m = 1; m <= 2; m++) { | |
const size = Math.pow(sizeBase, m); | |
const content = Buffer.alloc(size, "0", "utf-8"); | |
const unit = | |
m === 1 ? "K" : | |
m === 2 ? "M" : | |
m === 3 ? "G" : "never"; | |
console.log(`===Test read,write,unlink file of 1${unit} content===`); | |
console.time(`fori + await 1${unit}`); | |
for (let i = 0; i < testCount; i++) { | |
const filename = `zero${i}.txt`; | |
await writeFile(filename, content); | |
await readFile(filename); | |
await unlink(filename); | |
} | |
console.timeEnd(`fori + await 1${unit}`); | |
const emptyArr = Array.from(Array(testCount)).map((_, i) => i); | |
const concurrencyBase = 2; | |
for (let mm = 1; mm <= 6; mm++) { | |
const concurrency = Math.pow(concurrencyBase, mm); | |
console.time(`pMap ${concurrency} concurrency 1${unit}`); | |
await pMap(emptyArr, async i => { | |
const filename = `zero${i}.txt`; | |
await writeFile(filename, content); | |
await readFile(filename); | |
await unlink(filename); | |
}, { concurrency }); | |
console.timeEnd(`pMap ${concurrency} concurrency 1${unit}`); | |
} | |
console.time(`fori + sync 1${unit}`); | |
for (let i = 0; i < testCount; i++) { | |
const filename = `zero${i}.txt`; | |
writeFileSync(filename, content); | |
readFileSync(filename); | |
unlinkSync(filename); | |
} | |
console.timeEnd(`fori + sync 1${unit}`); | |
} | |
} |
Author
piglovesyou
commented
Mar 6, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment