craftyjs/Crafty — pull request #430 by starwed (2013-03-07 00:30:36+00:00)
Title: Hashmap revision

These patches rewrite portions of Crafty's Hashmap. Changes:

* Uses a bit pattern as a coordinate key instead of a string. This is faster and creates less garbage, but would break a scene that involved more than 65k cells. (With the default settings, that would be 2 million pixels.) I don't this is a real concern, but enlighten me if I'm wrong! :)

* Add a function to refresh an objects position in the hashmap, rather than removing it and then adding it back. The old way created a lot of garbage. (I imagine the refresh function could also be further optimized.)

* Where possible, avoid creating objects by reusing existing ones. Every use of mbr() makes Firefox's GC cry, and we don't want that.

I've used these changes for a long time in a game I wrote, so they seem fairly bug free. My own benchmarking showed a general performance improvement. I ran some jsperf tests on [encoding](http://jsperf.com/string-vs-bit-hash/2) and [decoding](http://jsperf.com/hash-reversal-for-bits-and-strings) the hash, and the bitwise version won out with both, but the most noticeable gain was the reduction in GC pauses in Firefox.