create 1000 txt file.
calc 10 times runtime.
Last active
January 11, 2021 10:22
-
-
Save bishil06/501782d11bef30edd9108c7f4269690a to your computer and use it in GitHub Desktop.
Node.JS read directory
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 path = require('path'); | |
const fs = require('fs'); | |
const fsPromises = fs.promises; | |
let allPromises = []; | |
const data = new Uint8Array(Buffer.from('Hello Node.js')); | |
for(let i=1; i<=1000; i++) { | |
let fileName = `sample${i}.txt`; | |
let filePath = path.join('../testDir/readfilelist_test/', fileName); | |
let f = fsPromises.writeFile(filePath, data); | |
allPromises.push(f); | |
} | |
Promise.all(allPromises); | |
// create file sample1.txt ~ sample1000.txt to '../testDir/readfilelist_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
... | |
sample261.txt sample442.txt sample623.txt sample804.txt sample986.txt | |
sample262.txt sample443.txt sample624.txt sample805.txt sample987.txt | |
sample263.txt sample444.txt sample625.txt sample806.txt sample988.txt | |
sample264.txt sample445.txt sample626.txt sample807.txt sample989.txt | |
sample265.txt sample446.txt sample627.txt sample808.txt sample99.txt | |
sample266.txt sample447.txt sample628.txt sample809.txt sample990.txt | |
sample267.txt sample448.txt sample629.txt sample81.txt sample991.txt | |
sample268.txt sample449.txt sample63.txt sample810.txt sample992.txt | |
sample269.txt sample45.txt sample630.txt sample811.txt sample993.txt | |
sample27.txt sample450.txt sample631.txt sample812.txt sample994.txt | |
sample270.txt sample451.txt sample632.txt sample813.txt sample995.txt | |
sample271.txt sample452.txt sample633.txt sample814.txt sample996.txt | |
sample272.txt sample453.txt sample634.txt sample815.txt sample997.txt | |
sample273.txt sample454.txt sample635.txt sample816.txt sample998.txt | |
sample274.txt sample455.txt sample636.txt sample817.txt sample999.txt | |
sample275.txt sample456.txt sample637.txt sample818.txt | |
sample276.txt sample457.txt sample638.txt sample819.txt | |
sample277.txt sample458.txt sample639.txt sample82.txt | |
sample278.txt sample459.txt sample64.txt sample820.txt |
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 fsPromises = fs.promises; | |
async function opendir(path) { | |
let dir = await fsPromises.opendir(path); | |
for await (const dirent of dir) { | |
// console.log(dirent); | |
} | |
} | |
// 10 times test | |
async function test() { | |
for(let i=0; i<10; i++) { | |
console.time('opendir'); | |
await opendir('../testDir/readfilelist_test/'); | |
console.timeEnd('opendir'); | |
} | |
} | |
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
opendir: 7.571ms | |
opendir: 3.567ms | |
opendir: 6.059ms | |
opendir: 5.12ms | |
opendir: 2.688ms | |
opendir: 2.341ms | |
opendir: 4.05ms | |
opendir: 1.899ms | |
opendir: 2.151ms | |
opendir: 1.863ms |
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 fsPromises = fs.promises; | |
async function readdir(path) { | |
let dirents = await fsPromises.readdir(path, {withFileTypes:true}) | |
for(const dirent of dirents) { | |
// console.log(dirent); | |
} | |
} | |
async function test() { | |
for(let i=0; i<10; i++) { | |
console.time('readdir'); | |
await readdir('../testDir/readfilelist_test/'); | |
console.timeEnd('readdir'); | |
} | |
} | |
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
readdir: 2.6ms | |
readdir: 2.079ms | |
readdir: 1.24ms | |
readdir: 1.123ms | |
readdir: 1.094ms | |
readdir: 1.198ms | |
readdir: 1.138ms | |
readdir: 1.134ms | |
readdir: 3.417ms | |
readdir: 1.134ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment