Skip to content

Instantly share code, notes, and snippets.

@YonatanKra
Last active March 21, 2020 04:19
Show Gist options
  • Save YonatanKra/a6dd6b287c0949602231cd373b6777fb to your computer and use it in GitHub Desktop.
Save YonatanKra/a6dd6b287c0949602231cd373b6777fb to your computer and use it in GitHub Desktop.
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