Skip to content

Instantly share code, notes, and snippets.

@YonatanKra

YonatanKra/logMemory.js

Last active Mar 21, 2020
Embed
What would you like to do?
function logMemory() {
if (typeof process != 'undefined') {
console.log(`Node: ${process.memoryUsage().heapUsed / Math.pow(1000, 2)} MB`);
} else if (performance) {
console.log(`Browser: ${performance.memory.usedJSHeapSize / Math.pow(1000, 2)} MB`);
} else {
throw ('Where d-heck are you trying to run me?');
}
}
function measureMemory() {
const arraySize = 25 * Math.pow(1000, 2);
logMemory();
(function() {
const array1 = new Array(arraySize).fill(1.1);
logMemory();
})();
(function() {
const array2 = new Array(arraySize).fill(1);
logMemory()
})();
setTimeout(() => {
logMemory();
}, 5000);
}
measureMemory();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.