cocos2d-js TMXTiledMap memory usage

The tilemaps created by cc.TMXTiledMap seem to have rather large memory footprints. For example, here’s a screenshot of 3 heap snapshots in chrome.

The first one is after starting the game. The second one is after executing a single line of code in the debugger:

var map = new cc.TMXTiledMap(filename);

Which loads a 20x20 Tilemap with 5 layers. Same for the third heap snapshot, only that this time the loaded map was bigger (something like 80x20x5).

So the delta from Snapshot1 to Snapshot 2 is roughly 3 megabytes. That’s about 1.5 kilobyte of information per tile. I don’t know what’s going on here, but that’s way too much. I’m assuming this is because every tile in the tilemap is a cc.Sprite, and has all the accompanying data attached to it.

I’m not sure there is a way to effectively optimize this, but for future Cocos2d-JS versions, consider refactoring the whole thing to not represent a single tile as a complete cc.Sprite. Way too much duplicated or flat out unnecessary data there.

Yep, tilemap is a heavy class. The new c++ TMXFastTileMap and TMXFastTileLayer do what you suggest and only create indexed quads using the primitive command instead of instances of sprite. Feel free to refactor the JS version for the community.

I don’t know enough about the non-JSB version of cc.TMXTiledMap to know if you could easily change it, but if you’re using JSB look into FastTileMap.