let data = [];
function fetchData() {
// Simulates fetching data from a server
const newItems = new Array(10000).fill().map(() => ({
id: Math.random(),
value: Math.random()
}));
// Append new items to existing data
data = [...data, ...newItems];
display(data);
}
// Call this function repeatedly
setInterval(fetchData, 5000);
This code creates an unbounded growing array over time: 1) Each call to fetchData adds 10,000 new items to the data array, 2) The array continuously grows because data is never cleared or limited, 3) With regular calls via setInterval, this creates an ever-increasing memory consumption, 4) This pattern will eventually lead to performance degradation or crashes, 5) A better approach would be implementing a sliding window, limiting the array size, or clearing unnecessary old data, 6) This is a common pattern in real-time data applications that can lead to memory issues, 7) Memory leaks of this nature are particularly dangerous because they accumulate slowly over time, 8) Implementing proper data lifecycle management is essential for long-running applications.