function formatMemoryUsage(bytes) {
return `${(bytes / 1024 / 1024).toFixed(2)} MB`;
}
function logMemoryUsage() {
const usage = process.memoryUsage();
console.log(`Memory Usage: Heap Total: ${formatMemoryUsage(usage.heapTotal)}, Heap Used: ${formatMemoryUsage(usage.heapUsed)}`);
}
// Simulate string slicing operations
function simulateStringSlicing() {
let str = 'a'.repeat(10 * 1024 * 1024); // 10 MB string
let slicedArray = [];
// Simulate slicing
for (let i = 0; i < 1000; i++) {
let sliced = str.slice(i, i + Math.floor(Math.random() * 100));
slicedArray.push(sliced);
}
}
let count = 0;
const intervalId = setInterval(() => {
simulateStringSlicing();
logMemoryUsage();
count++;
if (count === 25) {
clearInterval(intervalId); // Stop the interval after 25 executions
}
}, 1000);