Here is a solution that wraps the readFileSync
with openSync
and closeSync
:
import { openSync, closeSync, readFileSync, writeFileSync } from 'fs'
const d1 = openSync('input.json', 'r')
const data = readFileSync(d1).toString();
closeSync(d1)
const parsedData = JSON.parse(data)
const filenames = new Array<string>();
for (const n of parsedData) {
filenames.push(n['name']);
}
let mydata = [];
for (const filename of filenames) {
const d = openSync(filename, 'r')
mydata.push(readFileSync(d).toString())
closeSync(d)
}
console.log(mydata.length)
It opens + reads + closes 100,000 files in a loops without any problem:
$ grep -rn "\"name\": " input.json | wc -l
100001
$ npx tsc main.ts
$ node ./main.js
100001