spleen387 Posted March 22, 2014 Share Posted March 22, 2014 So I'm putting together some code for the low level of my next game and so far I have a constructor for constructing game object constructors (createGameObjectConstructor)and it's child: Clock, which will be used to set up the main loop and such. (two others Monster and Vec3 but they're just test cases) everything, to my knowledge right now, is working just fine but I was wondering if you guys could have a look over it to see if theres anything I could do a better way,particularly in createGameObjectConstructor theres an Object (outerProto) that gets created everytime createGameObjectConstructor is called but I cant seem to find a way around that.. thanks! http://jsfiddle.net/hv7Xx/1/orhttps://gist.github.com/spleen387/9713289 Quote Link to comment Share on other sites More sharing options...
away168 Posted March 23, 2014 Share Posted March 23, 2014 Please correct me if I'm wrong about your code cause I'm just having a quick-read and didn't test it. I think it is quite complex but also you are specializing the object creator (as game object constructor). It is best that object creation is generic, as everything may have the same trouble as they are "object". If it is only one level of inheritance and doesn't need namespace, it doesn't need any object creation pattern at all (just use function A() {}). About the performance, even though the process won't be seen by the gameplay developer, I see that ret() function is invoked everytime an object has to be created, and inside that you call a lot of stuff that is expensive such as .apply, recentArray = [] (Careful you're creating a new array instance here!), and attaching a new property, for the sake of pooling if I see it (recycling the instance). These may causes an overhead when allocating a new object. If you want to do an object creation, maybe try to trim the code not to have anything attached to the instance (the countDescriptor I guess). More properties attached to an instance means more performance degradation in JavaScript. Google has suggested not to have more than 30 properties, or keep is as minimum as possible (Unless the properties are class members, which means that the class is always only one.) I suggest that as far as you go, try to trim everything down to compilation mode. For example, make sure whatever you do before the runtime (calling the main/onload), all those codes have been supplied to each object (in your case, gameobject), and when it is needed to be used, it will be used natively (var car = new Car()). This will save the performance during run time, as it uses the native "new". Also with that, the object is easily available for generic or specialized object pooling outside your recycler. You might want to think about some of these issues if you're talking about these low level things; namespaces, circular dependencies (A has B, B has A), and object comparison (a instanceof A? a === A?).And if you want to test it the performance, simply do a 100K - 1M loop test of yours against the native "new". If performance is not an issue, then your code is fine as long as you make it generic (I guess it's probably just the naming convention). But looking that you're using a recycler like that, I assume you want better performance. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.