joel.
> Once upon a time, I was working on a Tetris Dashboard Widget for Mac OS
> X. Following a couple suggestions I found online about "extending"
> JavaScript objects, I ended up creating getters and setters for a lot
> of properties. I continued this method throughout the project even
> though I sort of wished I didn't. I started learning my lesson near the
> end when the game was running slower than I had imagined possible.
> Using an option in Safari's debug menu, I noticed that the number of
> allocated objects in memory was around the 80,000 mark or so. It didn't
> take to long to figure out that every time a reference (or object, or
> whatever JavaScript does) is passed around, that number went up.
> For some reason, that number didn't seem to come down very quickly
> either, and it would just grow and grow and grow. My solution
> eventually led to around 100 new objects allocated per new Tetris piece
> instead of several thousand!!! woah!!! In the end, I never really
> topped 4,000 total objects in memory (and that number started around
> 1,500). Those numbers may be a LITTLE off, but the difference is so
> significant, I didn't think it's matter to be right on.
> Simply removing the getters and setters was enough to bring the number
> of objects in memory down substantially. Another big improvement came
> from removing some stupid functions I was using from a code snippet
> online that actually created the getters and setters for me, which I
> remember attributing to another memory allocation every time I used a
> getter or setter (that was a minimum of 3 just by calling a get, since
> 1 was the object itself!).
> Anyway, I deleted the whole thing and started over just for fun. This
> time ignoring all the online suggestions, it ran smooth as silk when I
> was finished. (Actually I never really FINISHED the game, but it does
> work. I usually get bored near the end, quit, and invent some other
> idea to waste time on--so no Tetris Dashboard widget yet).
> By accessing variables in objects directly instead of through getters
> and setters improved performance immensely, and there were definitely a
> couple other things too. If I ever get the time, I may look for those
> sites I found (highly popular Google search results by the way) and
> blog them to death because of all the frustration they caused me.
> Getters and setters in Java are different, but I believe GWT compiles
> them into JavaScript functions just the same. Could the compiler just
> not create those functions when appropriate?
> I am confident that it would increase performance in the case where a
> large program was written like Tetris, but I'm not sure. If anything,
> it would cut back on the amount of JavaScript generated.