1

I'm working on a javascript game for fun, where I have an array of character objects. I'm periodically doing checks on fields within the objects, (like characters[i].xpos, characters[i].ypos, and so on). Is it more efficient to add a function to the prototype of the class to handle moving left and right, and call that periodically, or more efficient to have a global function that adjusts the x and y pos of the character object?

Thanks for your help guys!


Edit: Thanks for your answers guys. I'll try to be more specific. I have one global update function that is called with an interval and has a bunch of code in it that changes the properties of an object, like:

globalupdate() {
  characters[i].posx++;
  characters[i].posy++;
  ... and more code like that
}

Would it be faster to just write a prototype function within that class and call that? Like:

globalupdate() {
   characters[i].update();
}

Thanks for all the quick replies!

2
  • This depends on your definition of efficiency. If might be microseconds quicker to directly update your objects properties from outside, but you may lose weeks trying to debug the code later. Generally I'd advise focus on good design rather than raw speed - and worry about optimising later. Commented Jul 30, 2011 at 17:53
  • @navr91: I have updated my answer. Have a look please. Commented Jul 31, 2011 at 9:57

2 Answers 2

1

I have just read an article on that subject by John Resig (author of jQuery). Judging from that, the most important thing is to not have multiple timers (i.e. intervals and timeouts) because there is a potential for conflict.

So, if your global function will result in having less timers this might very well be beneficial to the overall performance.


Edit: Do I understand your update correctly and is this what you want to know: given a block of code,

{
    object1.<key> = <value> ;
    object2.<key> = <value> ;
    /* ... */
    objectN.<key> = <value> ;
}

and an alternative block of code,

{
    object.prototype.change = function( ) { this.<key> = <value> ; } ;

    list = [object1,object2,/* ... */,objectN] ;
    for(i = 0 ; i < list.length ; i++) list[i].change( ) ;

}

which one would be executing faster?

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, that's the one! Is there any real difference? I was concerned because I thought maybe in the first example, the interpreter would have to move from the global scope to the scope of the object over and over for each line, which may slow things down (I'm not quite sure if this is how Javascript works, so please be forgive if that is an idiotic statement). Again, I'm not too sure about how the lower levels of Javascript work, though.
Well, to be honest I don't really know. The easiest way to find out is to time both patterns and compare the result. Writing a loop instead of a set of statements usually also makes your code more succinct and transparent which is obviously a good thing to do.
I just got over 15 rep points, so I'm voting up your answer. Thanks for your help!
0

As for all performance questions where it only takes 1 min to code together a test for each case, try.

I think the difference is negligble compared to the "visible" part of the update, ie updating the DOM etc. Also remember that it's probably going to be differences depending on which browser that is used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.