Last active
December 2, 2020 09:41
-
-
Save ChALkeR/f39029f2187d1ab81fa199ab221ca639 to your computer and use it in GitHub Desktop.
Stream memory usage
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
'use strict'; | |
const fs = require('fs'); | |
fs.writeFileSync('test-small.bin', Buffer.alloc(100)); | |
fs.writeFileSync('test-large.bin', Buffer.alloc(100000)); | |
const blockMaxSize = 4 * 1024 * 1024; // 4 MiB | |
const out = { buf: [], length: 0 }; | |
let built = 0; | |
function tick() { | |
built++; | |
if (built % 1000 === 0) { | |
console.log(`RSS [${built}]: ${process.memoryUsage().rss / 1024 / 1024} MiB`); | |
} | |
let stream; | |
if (built % 2 === 0) { | |
const stream = fs.createReadStream('./test-small.bin'); | |
stream.on('data', function (data) { | |
//data = Buffer.from(data); // WARNING: uncommenting this line fixes things somehow | |
out.buf.push(data) | |
out.length += data.length | |
if (out.length >= blockMaxSize) { | |
out.buf = [] | |
out.length = 0 | |
} | |
}); | |
stream.once('end', tick); | |
} else { | |
const stream = fs.createReadStream('./test-large.bin'); | |
stream.on('data', function() {}); | |
stream.once('end', tick); | |
/* Alternatively: | |
fs.readFileSync('./test-large.bin'); | |
setImmediate(tick); | |
*/ | |
} | |
} | |
tick(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment