Following last week’s investigations I tried moving all of the functions inside the EntityPassive object (closure) to the outside, to be attached as external prototype functions. As I discovered, in keeping with what I’ve read, attaching the functions outside of the object results in a much smaller memory footprint. With internal functions the object took up 144 bytes and with external functions it consumed 480. There are other differences as well, as indicated by the before-and-after memory comparisons generated using the Memory Inspector in Firefox.
480 bytes, by the way, is a 224-byte base allocation plus a 64-byte incremental allocation plus a 128-byte allocation, as I described in my last post. You might want to keep this mechanism in the back of your mind if you’re working on a memory-intensive application, particularly when the objects are larger or there are large numbers of objects to be allocated.
Here’s the usage for internally declared functions:
Here’s the usage for externally declared functions:
The code runs without a hitch either way.
One caution about using the Memory Inspector in Firefox: you should probably take at least two snapshots of every state. I’ve found that the first pass doesn’t generate consistent information but subsequent passes seem to get a more accurate read on what’s there.