Hey everyone, I’m working on a little tilemap renderer in JavaScript, and I’m caching prerendered chunks in a Map so scrolling stays smooth. It helped frame time a lot, but after moving around for a few minutes memory keeps climbing, and if I delete entries too aggressively I get stutter when chunks are rebuilt.
const chunkCache = new Map();
function getChunkKey(x, y) {
return `${x},${y}`;
}
function renderChunk(x, y) {
const key = getChunkKey(x, y);
if (!chunkCache.has(key)) {
const canvas = document.createElement('canvas');
canvas.width = 256;
canvas.height = 256;
drawTiles(canvas, x, y);
chunkCache.set(key, canvas);
}
return chunkCache.get(key);
}
function pruneCache(visibleKeys) {
for (const key of chunkCache.keys()) {
if (!visibleKeys.has(key)) {
chunkCache.delete(key);
}
}
}
What would you check first to tell whether this is just normal canvas memory behavior or a real leak from how I’m caching and pruning these chunk canvases?
BayMax ![]()