Skip to content

Instantly share code, notes, and snippets.

@wermarter
Created September 27, 2023 02:13
Show Gist options
  • Save wermarter/1985023d4fa93dbe955a4591315a8453 to your computer and use it in GitHub Desktop.
Save wermarter/1985023d4fa93dbe955a4591315a8453 to your computer and use it in GitHub Desktop.
javascript generator vs stream API
const nReadlines = require("n-readlines");
const fs = require("fs");
const events = require('events');
main().then(() => {
const used = process.memoryUsage().heapUsed / 1024 / 1024;
console.log(
`The script uses approximately ${Math.round(used * 100) / 100} MB`
);
process.exit(0);
});
////////////////////////////////////////////////////////////////
function* nReadCursor(filename) {
const broadbandLines = new nReadlines("test.sql");
while ((line = broadbandLines.next())) {
yield line;
}
}
function useGenerator() {
let counter = 0;
for (const line of nReadCursor("test.sql")) {
// counter++;
// if (counter % 100000 === 0) {
// console.log(counter);
// }
// console.log(line);
}
}
////////////////////////////////////////////////////////////////
async function readFilestream(filename, cb) {
reader = fs.createReadStream("test.sql");
reader.on("data", function (chunk) {
cb(chunk.toString());
});
reader
await events.once(reader, 'close');
}
async function useStream() {
let counter = 0;
await readFilestream("test.sql", function (data) {
// console.log(data)
});
}
////////////////////////////////////////////////////////////////
async function main() {
// await useStream()
useGenerator()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment