getify / cloud-sweeper

A casual game built for the web.
94 stars 17 forks source link

refactor: use the constructor pattern on frequently made objects (perf) #25

Open getify opened 8 years ago

getify commented 8 years ago

I'm not a big fan of the constructor pattern, as most people who follow me know.

But it does seem to help when objects have a predictable shape and are created frequently. NOTE: this has nothing to do with using classes as a design pattern. It's a way to declare the shape of the object in a way that the engine can pre-optimize for.

ex:

// instead of:
var obj = { x: .., y: .., z: .. }

// do:
function makeObj() {
   this.x = ..
   this.y = ..
   this.z = ..
}
var obj = new makeObj();
joekarl commented 8 years ago

I'm curious if you pinned this as actually causing perf problems. I wouldn't think you're creating too many objects throughout the course of your gameloop.

getify commented 8 years ago

TBH, i don't really have the devtools skills sufficient to properly benchmark such things.

I filed this by intuition, because I am creating a couple dozen objects (and them re-using them in a pool) and they have a predictable shape. So figured i could save a little cpu by moving the object literals for those to constructors.

Compared to some of the other perf refactorings discussed in other threads, this part is a relatively simple change. I don't like constructors or this pattern, but it's clear in perf-sensitive cases it helps engines like v8.

NOTE: I'd be very happy if someone more skilled with profiling could help me tease apart the code perf as it stands to help me understand where the biggest gains can be made. Ofc, I want to balance that with amount of effort to refactor (and the style of code that will result in the end), but it would be nice to have more than just intuition. :)

joekarl commented 8 years ago

From looking at some profiles as well as your code/overall structure of the game, I seriously doubt that you even need to pay attention to this right now. Even on the desktop most of your time per frame is spent in your rendering code (specifically drawing images) so that's gonna be where the biggest gains can be made. I think this is probably a nice to have to make you feel good about tuning every last thing but tuning your rendering is probably a more important place to put your efforts.

cloud-sweeper-prof

getify commented 8 years ago

thanks for the fantastic info/analysis! :)

getify commented 8 years ago

i will also mention that though i may not be creating enough objects yet for this optimization to matter much, the plans for future extension of the gameplay will likely significantly increase the object creation load. it may be useful to make this optimization sooner so as to set the right precedent for future code.

joekarl commented 8 years ago

Yeah, also with named constructor type things you'll get more meaningful profiles (right now comparing heap snapshots gives you not great results because your objects just show up as "Object" in the heap profile)

getify commented 8 years ago

:+1: