Skip to content

Commit

Permalink
docs: reproduce env from issue #361
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Oct 5, 2023
1 parent 7fd34c5 commit 63c4ac6
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions demo/issues-esm/labo/361-transform-memory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fs from "fs";
import { pipeline } from "stream/promises";
import { transform } from "stream-transform";
import desm from 'desm';
const dirname = desm(import.meta.url);

const format = (data) =>
`${Math.round((data / 1024 / 1024) * 100) / 100} MB`;
let maxUsedHeap = 0;
async function main() {
setInterval(() => {
const stats = process.memoryUsage();
maxUsedHeap = Math.max(maxUsedHeap, stats.heapUsed);
console.log(`head used: ${format(stats.heapUsed)}; max heap used: ${format(maxUsedHeap)}`)
}, 1000);

await pipeline(
function* () {
let i = -1;
// Original code with a record limit
// const n = 9999999;
// while (++i < n) {
// yield { i };
// }
// Run with unlimited records
while (true) {
i++
yield { i };
}
},
transform({ parallel: +process.env.PARALLEL }, (chunk, next) =>
next(null, chunk.i)
),
fs.createWriteStream(`${dirname}/361-transform-memory.csv`)
);

console.log(`${maxUsedHeap / (1000 * 1000)}mb`);
}

main();

// $ PARALLEL=1 node example.js
// 6.009856mb

// $ PARALLEL=2 node example.js
// 320.684144mb

0 comments on commit 63c4ac6

Please sign in to comment.