Skip to content

Instantly share code, notes, and snippets.

@amitzur
Created September 1, 2019 13:14
Show Gist options
  • Save amitzur/daadfdf280c2d893ac58a8009b28154b to your computer and use it in GitHub Desktop.
Save amitzur/daadfdf280c2d893ac58a8009b28154b to your computer and use it in GitHub Desktop.
memory usage
const toMB = require('./toMB')
console.log(`[0]\t${takeMem()}`)
const MB = 1024 * 1024;
const MAX = MB * 200;
let s = '', i = -1;
const buff = Buffer.alloc(MAX);
const start = Date.now();
while (++i < MAX) {
// s += 'a';
buff.write('a', i)
// if (s.length === 1000000) console.log(s.length)
if (!(i % MB)) {
console.log(`[${toMB(i)}]\t${takeMem()}`)
}
}
const end = Date.now()
console.log(`${(end - start) / 1000} ms, ${toMB(buff.toString().length)}`);
function takeMem() {
const used = process.memoryUsage();
return Object.keys(used).map(key => `${key} ${toMB(used[key])}`).join('\n\t')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment